I use DateTime in MySQL. So I have something like 2014年10月31日 00:00:00 in my database.
From my API, when I return some query, I would normally have to loop through them and convert the DateTime to Timestamp using strtotime(). Then in JavaScript, I can use it by multiplying this timestamp by a 1000.
However, is there a way I could pass the pure DateTime to Javascript and do all the conversion there?
-
1) You could also let MySQL do the conversion 2) Take a look at XDate.js, which provides date string into date object parsing.Niko– Niko2014年10月30日 22:51:06 +00:00Commented Oct 30, 2014 at 22:51
-
I don't want to use an entire library for only a simple conversion! Is the conversation actually this complicated?Kousha– Kousha2014年10月30日 22:56:13 +00:00Commented Oct 30, 2014 at 22:56
-
@Kousha Spend any time working with computers and time math and you'll find it's far more complicated than it seems. As an example, from 1909 to 1937 the Netherlands was exactly 19 minutes and 32.13 seconds ahead of UTC by law.ceejayoz– ceejayoz2014年10月30日 22:59:08 +00:00Commented Oct 30, 2014 at 22:59
1 Answer 1
You can make MySQL do the work:
SELECT (UNIX_TIMESTAMP(date_field) * 1000) AS jsTimestamp FROM table;
Alternatively, Moment is pretty much the gold standard for JavaScript date work and can read MySQL-style strings right out of the box.