As new Date().toISOString() will return current UTC time, to get local time in ISO String format we have to get time from new Date() function like the following method
document.write(new Date(new Date().toString().split('GMT')[0]+' UTC').toISOString().split('.')[0]);
Answer from jafarbtech on Stack OverflowAs new Date().toISOString() will return current UTC time, to get local time in ISO String format we have to get time from new Date() function like the following method
document.write(new Date(new Date().toString().split('GMT')[0]+' UTC').toISOString().split('.')[0]);
You can use moment.js library to achieve this.
Try:
var moment = require('moment')
let dateNow = moment().format('YYYY-MM-DDTHH:MM:SS')
Format date at 'YYYY:MM:DD HH:MM;SS'
JS date and Time Format - JavaScript - SitePoint Forums | Web Development & Design Community
How to format a Date in MM/dd/yyyy HH:mm:ss format in JavaScript? - Stack Overflow
Should we always output an ISO date to JavaScript using ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'")?
Videos
[Addendum 12/2022]: Here's a library to format dates using Intl.DateTimeFormat.
[Addendum 01/2024]: And here is a (ES-)Date manipulation library
Try something like this
var d = new Date,
dformat = [d.getMonth()+1,
d.getDate(),
d.getFullYear()].join('/')+' '+
[d.getHours(),
d.getMinutes(),
d.getSeconds()].join(':');
If you want leading zero's for values < 10, use this number extension
Number.prototype.padLeft = function(base,chr){
var len = (String(base || 10).length - String(this).length)+1;
return len > 0? new Array(len).join(chr || '0')+this : this;
}
// usage
//=> 3..padLeft() => '03'
//=> 3..padLeft(100,'-') => '--3'
Applied to the previous code:
var d = new Date,
dformat = [(d.getMonth()+1).padLeft(),
d.getDate().padLeft(),
d.getFullYear()].join('/') +' ' +
[d.getHours().padLeft(),
d.getMinutes().padLeft(),
d.getSeconds().padLeft()].join(':');
//=> dformat => '05/17/2012 10:52:21'
See this code in [jsfiddle][1]
[edit 2019] Using ES20xx, you can use a template literal and the new padStart string extension.
const dt = new Date();
const padL = (nr, len = 2, chr = `0`) => `${nr}`.padStart(2, chr);
console.log(`${
padL(dt.getMonth()+1)}/${
padL(dt.getDate())}/${
dt.getFullYear()} ${
padL(dt.getHours())}:${
padL(dt.getMinutes())}:${
padL(dt.getSeconds())}`
);
You can always format a date by extracting the parts and combine them using string functions in desired order:
function formatDate(date) {
let datePart = [
date.getMonth() + 1,
date.getDate(),
date.getFullYear()
].map((n, i) => n.toString().padStart(i === 2 ? 4 : 2, "0")).join("/");
let timePart = [
date.getHours(),
date.getMinutes(),
date.getSeconds()
].map((n, i) => n.toString().padStart(2, "0")).join(":");
return datePart + " " + timePart;
}
let date = new Date();
console.log("%o => %s", date, formatDate(date));
Was chatting with chatgpt about how date format outputs can go wrong between back end and javascript, and I'd just like to have this confirmed by humans if possible. :)
The first point was that JS is only accuate to milliseconds, so using
ToString("o")will silently lose precision (from 7 digits to 3 digits) when interpreted in JS.e.g. Output from
ToString("o"):2025-11-25T01:10:28.4156402+00:00Javascript only sees milliseconds:
2025-11-25T01:10:28.415+00:00Apparently some older browsers / devices have historically sometimes completely failed to parse a string more than 3 digit precision, but mainly the round-trip value will be different, not exactly the same. Hence it is "safer" to explicitly use "yyyy-MM-ddTHH:mm:ss.fffZ", to say "just note we only have 3 digit precision to work with here".
The second point was that single quotes should be around the
'T'and'Z'is also safer because theToString()parser doesn't actually recognise T and Z as ISO date tokens at all - it sees them as "invalid string format tokens" and outputs them as-is - which means potentially that behaviour might change in future. (One can use "+00:00" instead of "Z" but single quotes are still needed around the "T".)
Bottom line, it seems the "safest" format string to serialise a date for javascript is "yyyy-MM-dd'T'HH:mm:ss.fff'Z'" (or "yyyy-MM-dd'T'HH:mm:ss.fff+00:00"), with the single quotes and explicitly stating 3-digit precision.
So is that what everyone does when serialising for javascript, or are there also other, equally round-trip sate, ways? i.e. Should I go ahead and adopt this as standard, or are there other considerations?
(ed formatting)
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>
try using moment js for that. look at: moment.js parse with format for example:
var mydate = moment("2016-11-16 00:00:00.000", "yyyy-MM-dd HH:mm:ss.SSS").toDate();
didn't test the format I put there as moment.js might use different strings to define it but you can see it all in the doc link I wrote above