I need help converting this date format.
Here are the date string I receive from a server, it can be M/DD/YYYY or M/D/YYYY or MM/D/YYYY or MM/DD/YYYY.
EDIT: Sorry, my bad. After the format above, there is additional string of time HH:MM:SS AM so the complete string I receive will be M/D/YYYY HH:MM:SS AM
My problem now is I use String.substring(x, y) to extract the date, month, and year.
So far I only managed to determine the location of “/” but I’m still thinking how to use it in the substring method.
var separators = [];
for(var b = 0, c = String.length; b < c; b++){
if(String[b] === '/'){
separators.push(b);
}
}
I’m confused how to extract the date and month dynamically and then pad “0” in front of the single digit number, so I appreciate any helps. Thank you in advance.
it works with any of the above format –
M/DD/YYYYorM/D/YYYYorMM/D/YYYYorMM/DD/YYYY– (and it’s easier than using substring)then to add a pad to month and day just use
slice()like soand to get a padded data just re-join the array parts
Example jsbin : http://jsbin.com/exodos/2/edit