Created
June 9, 2014 07:27
-
-
Save lyonsun/077702a30aa84fc5e6f5 to your computer and use it in GitHub Desktop.
parse datestring in format 'YYYY-MM-DD HH:II:SS' to timestamp in millionseconds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parseDate(dateString) { | |
var dateString, parts, date, time, dt, ms; | |
parts = dateString.split(/[T ]/); // Split on `T` or a space to get date and time | |
date = parts[0]; | |
time = parts[1]; | |
dt = new Date(); | |
parts = date.split(/[-\/]/); // Split date on - or / | |
dt.setFullYear(parseInt(parts[0], 10)); | |
dt.setMonth(parseInt(parts[1], 10) - 1); // Months start at 0 in JS | |
dt.setDate(parseInt(parts[2], 10)); | |
parts = time.split(/:/); // Split time on : | |
dt.setHours(parseInt(parts[0], 10)); | |
dt.setMinutes(parseInt(parts[1], 10)); | |
dt.setSeconds(parseInt(parts[2], 10)); | |
ms = dt.getTime(); // or ms = +dt; if you want to be l33t about it | |
return ms; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment