Wednesday, May 1, 2019

Javascript substring returns unexpected results

I'm trying to reformat the first date from a string with Javascript. The date begins in a UK format (dd/mm/yyyy), and needs to end up in ISO format (yyyy-mm-dd).




Here's my function:



function goSearch() {
var dr = $('input[name=daterange]').val(); // string is as follows: "05/03/2018 - 10/03/2018"
var st = dr.substr(0,10);
var ststring = st.replace(/\//g, '-'); // string is now: "05-03-2018"
var stdd = ststring.substr(0,2); // produces "05"
var stmm = ststring.substr(3,5); // produces "03-20"
var styyyy = ststring.substr(6,10); // produces "2018"

var stIso = styyyy + "-" + stmm + "-" + stdd; // final date is "2018-04-20-08"
}


I can't work out why the variable "stmm" (which is equal to ststring.substr(3,5) ) returns "03-20" as opposed to "03".



Thanks in advance.

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...