I have a date formatted like this from a Java Server:
YYYY/MM/DD 00:00:00.0
And that is a format that JavaScript won’t accept. So, I do this:
var startDate = '2012-07-27 00:00:00.0';
startDate = startDate.substring(0, 10);
Which then kindly returns 2012-07-27. (Note: I tried it with the time and without)
However, then I want to turn it into a date format I can use to compare two dates. So, I preform this:
startDate = new Date(startDate);
Which, finally, returns this in the log: Invalid Date
So, for the purpose of comparing dates, like so:
if(currentDate > startDate)
I need to be able to get the format I have into one that will be accepted so I can then compare each of the various dates.
Thanks!
You string format is incompatible that is why new Date(datestring) won’t work. You can parse the string yourself with Regex and create a new Date object with explicit values for year, month, date, minute and second . Look at Date.parse for valid format
Or use this https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/UTC after parsing your string
example for your string would be