From f0ba793a8504818c16b0b9df11e6f41ff5b61d8e Mon Sep 17 00:00:00 2001 From: rolux Date: Mon, 4 Jun 2012 12:02:29 +0200 Subject: [PATCH] allow for passing milliseconds to Ox.parseDate --- source/Ox/js/Date.js | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/source/Ox/js/Date.js b/source/Ox/js/Date.js index d52c14ed..7bcc0402 100644 --- a/source/Ox/js/Date.js +++ b/source/Ox/js/Date.js @@ -265,29 +265,30 @@ Ox.makeYear = function(date, utc) { }; /*@ -Ox.parseDate Takes a string ('YYYY-MM-DD HH:MM:SS') and returns a date - str string - utc If true, Date is UTC - > +Ox.parseDate('1970-01-01 01:01:01', true) - 3661000 +Ox.parseDate Takes a string ('YYYY-MM-DD HH:MM:SS.MMM') and returns a date + string String + utc If true, date is UTC + > +Ox.parseDate('1970-01-01 01:01:01.01', true) + 3661010 > +Ox.parseDate('1970', true) 0 > Ox.parseDate('50', true).getUTCFullYear() 50 @*/ -// FIXME: Why no milliseconds? -Ox.parseDate = function(str, utc) { - var date = new Date(0), - defaults = [, 1, 1, 0, 0, 0], - values = /(-?\d+)-?(\d+)?-?(\d+)? ?(\d+)?:?(\d+)?:?(\d+)?/.exec(str); +Ox.parseDate = function(string, utc) { + var date, + defaults = [, 1, 1, 0, 0, 0, 0], + values = /(-?\d+)-?(\d+)?-?(\d+)? ?(\d+)?:?(\d+)?:?(\d+)?\.?(\d+)?/ + .exec(string).slice(1); if (values) { - values.shift(); + date = new Date(); values = values.map(function(v, i) { - return v || defaults[i]; + return v ? (i == 6 ? Ox.pad(v, 3, '0') : v) : defaults[i]; }); values[1]--; [ - 'FullYear', 'Month', 'Date', 'Hours', 'Minutes', 'Seconds' + 'FullYear', 'Month', 'Date', + 'Hours', 'Minutes', 'Seconds', 'Milliseconds' ].forEach(function(part, i) { Ox['set' + part](date, values[i], utc); });