使用日期和时间优化我的JavaScript代码

时间:2019-03-07 22:01:35

标签: javascript

我想学习如何以更熟练的方式完成此代码。

我是JavaScript的新手,我知道该怎么做,但是我想以一种简单的方式。

任何帮助将不胜感激。

var urlstart = "domain.com";
var urlend = "?blah";

var serverTime = "Thu Mar 07 11:23:06 PST 2019";

var st = serverTime.toLowerCase();

var m = st.slice(4,7);

 if ( m === "jan") { 
    var m = "01"
  }
  if (m === "feb") { 
    var m = "02"
  }
  if (m === "mar") { 
    var m = "03"
  }
  if (m === "apr") { 
    var m = "04"
  }
  if (m === "may") { 
    var m = "05"
  }
  if (m === "jun") { 
    var m = "06"
  }
  if (m === "jul") { 
    var m = "07"
  }
  if (m === "aug") { 
    var m = "08"
  }
  if (m === "sep") { 
    var m = "09"
  }
  if (m === "oct") { 
    var m = "10"
  }
  if (m === "nov") { 
    var m = "11"
  }
  if (m === "dec") { 
    var m = "12"
  }

var d = st.slice(8,10);

var y = st.slice(-2);

var t = st.slice(11,23);

var th = t.slice(0,2);
var th = Math.floor(th);

/* 9pm or After */
if (th > 20 ) { 

/* End Of Month - add 1 to Month value and change Day value 1 */
    if ( ( d == "30" && ( m == "04" || m == "06" || m == "09" || m == "11")) || 
    ( d == "31" && ( m == "01" || m == "03" || m == "05" || m == "07" || m == "08" || m == "10")) 
    || ( d == "28" && m == "02") ) {
    var m = Math.floor(m);
    var m = m + 1;
    var m = m.toString();
    var m = m.replace(/\d+/g, function(m){ return "0".substr(m.length - 1) + m; });
    var d = "01";
    window.location.href = urlstart+m+d+y+urlend;
    }

/* End Of the Year - Change M and Day and Year */
    else if ( m =="12" && d=="31" ) {
    var m = "01";
    var d = "01";
    var y = Math.floor(y);
    var y = y + 1;
    var y = y.toString();
    var y = y.replace(/\d+/g, function(y){ return "0".substr(y.length - 1) + y; });
    window.location.href = urlstart+m+d+y+urlend;
    }

else { /* Days that are not End Of Month */
var d = Math.floor(d);
var d = d + 1;
var d = d.toString();
var d = d.replace(/\d+/g, function(d){ return "0".substr(d.length - 1) + d; });

window.location.href = urlstart+m+d+y+urlend;

}

} else {

window.location.href = urlstart+m+d+y+urlend;

}

谢谢您的时间。 ................................................... ................................................... ............................................

0 个答案:

没有答案