0

I would like learn how this code can be done in a more proficient manner.

I am new to JavaScript and I know how to do things but in a simple way I guess.

Any help would be appreciated.

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;

}

Thank you for your time. ................................................................................................................................................

Buildingbrick
  • 107
  • 3
  • 14
  • 1
    What is this code trying to _do_ that you want to optimize? What is the initial value of `serverTime`? – Abion47 Mar 07 '19 at 22:04
  • There exist several date/time parsing libraries, you could use one of those, then you just may need to specify the format as something like `dd MMM yyyy` and you will get an object that has all relevant parts as properties. – H.B. Mar 07 '19 at 22:09
  • sorry! updated with serverTime var – Buildingbrick Mar 07 '19 at 23:11
  • Ok so you mean pulling something like an end of the month variable using a library instead of manually doing it? @H.B. – Buildingbrick Mar 07 '19 at 23:16
  • @Buildingbrick: You usually provide a format string, that specifies where which parts are and how they are written, e.g. `yyyy` would be 4 digit year, and the object you get back then has a property, maybe called `year`, that provides that part as a number. Then you can work with that object. If you need something like the end of the month, libraries sometimes provide utility functions that can help with such queries as well. – H.B. Mar 07 '19 at 23:59
  • You can change all `if()` to `else if()` because if you use `if()` all conditions will be check but for `else if()` just 1 or runs until a true condition is met. An other better code is [in here](https://stackoverflow.com/q/13566552/4229270) You can check that too. You can also optimise code by using [JS Date functions](https://www.w3schools.com/jsref/jsref_obj_date.asp). No need to slice from the string for month, date, time etc... – Sinto Mar 08 '19 at 04:57

0 Answers0