How can I convert UTC date-time string (e.g. 2011-03-29 17:06:21 UTC
) into Epoch (milliseconds) in javascript?
If this is not possible, is there any way to compare (like <, >) UTC date time strings?
How can I convert UTC date-time string (e.g. 2011-03-29 17:06:21 UTC
) into Epoch (milliseconds) in javascript?
If this is not possible, is there any way to compare (like <, >) UTC date time strings?
this is how to do it. No nonsese. Date.UTC accepts a UTC timestamp and returns epoch
Note that UTC date strings can be compared lexicographically, like strings, since the higher order values appear leftmost in the string.
You can extract the date fields from your example string and return the number of milliseconds by using the
Date.UTC
method:Using datejs will help you convert the UTC string to a Date object. After that it's simply a matter of calling .getTime() on the date object to get the milliseconds.
You could use
getDateFromFormat(dateValue, dateFormat)
(available here) like so:It returns the epoch time in milliseconds.
As long as the datetime string is something unambiguous like an ISO8601-ish format (i.e. not MM/DD/YYYY vs DD/MM/YYYY), you can just use the Date constructor to parse it and then Math.floor: