Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse yyyy-MM-dd HH:mm:ss.SSS format date in javascript?

Tags:

javascript

const time = '2016-11-16 00:00:00.000';
const date = new Date(time);
console.info(date);

It seems safari cannot parse yyyy-MM-dd HH:mm:ss.SSS format date( it returns NaN ) while chrome works fine, how can I parse this?

like image 346
Kim Avatar asked Nov 16 '16 15:11

Kim


People also ask

What does YYYY MM DD HH SS sssZ mean?

Date formatting Dates are formatted using the following format: "yyyy-MM-dd'T'hh:mm:ss'Z'" if in UTC or "yyyy-MM-dd'T'hh:mm:ss[+|-]hh:mm" otherwise. On the contrary to the time zone, by default the number of milliseconds is not displayed. However, when displayed, the format is: "yyyy-MM-dd'T'hh:mm:ss.

How do I format a date in JavaScript?

The toDateString() Method in JavaScript First three letters of the week day name. First three letters of the month name. Two digit day of the month, padded on the left a zero if necessary. Four digit year (at least), padded on the left with zeros if necessary.


1 Answers

I'd use momentjs for working with dates in javascript. Easy example:

var time = '2016-11-16 00:00:00.000';
var m = moment.utc(time, "YYYY-MM-DD HH:mm:ss.SSS");
console.log(m)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.16.0/moment-with-locales.min.js"></script>
like image 92
baao Avatar answered Sep 28 '22 03:09

baao