My configuration is Lisbon time zone. When I do new Date()
I get my current local date/time which is Fri Apr 28 2017 01:10:55 GMT+0100 (GMT Daylight Time)
When I get the ISO string with toISOString()
it will apply the time zone and I will get:
2017-04-28T00:10:55.964Z
The problem is that some minutes ago the date time was like this (yesterday):
2017-04-27T23:45:05.654Z
I tried moment.js (new to this) and I did something like this
document.write(moment('2017-04-28').format())
But I get this 2017-04-28T00:00:00+01:00
which is not 2017-04-28T00:00:00.000Z
I want to pass this value as a parameter of a restful method to parse it automatically as a DateTime type, but if I pass the output from moment.js format then it will get parsed as 2017-04-27 23:00:00.00
If I create a new date with new Date()
or with new Date('2017-04-27')
(date portion), I just want to get the ISO format like as follows with no time zone:
2017-04-28T00:00:00.000Z
Is there a javascript method like toISOString() or using maybe moment to get that format?
No matter what time zone or moment of day, I just to simulate that is midnight of the date given.
The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long ( YYYY-MM-DDTHH:mm:ss. sssZ or ±YYYYYY-MM-DDTHH:mm:ss. sssZ , respectively). The timezone is always zero UTC offset, as denoted by the suffix Z .
ISO 8601 represents date and time by starting with the year, followed by the month, the day, the hour, the minutes, seconds and milliseconds. For example, 2020-07-10 15:00:00.000, represents the 10th of July 2020 at 3 p.m. (in local time as there is no time zone offset specified—more on that below).
Time zones in ISO 8601 are represented as local time (with the location unspecified), as UTC, or as an offset from UTC.
ISO 8601. The format seen in your first example 2019-11-14T00:55:31.820Z is defined by the ISO 8601 standard. The T in the middle separates the year-month-day portion from the hour-minute-second portion. The Z on the end means UTC, that is, an offset-from-UTC of zero hours-minutes-seconds.
It's very unclear what you're asking. If you want the UTC date with the hours always 0, then set the UTC hours to 0 and use toISOString, e.g.
var d = new Date(); d.setUTCHours(0,0,0,0); console.log(d.toISOString());
Of course this is going to show the UTC date, which may be different to the date on the system that generated the Date.
Also,
new Date('2017-04-27').toISOString();
should return 2017-04-27T00:00:00Z (i.e. it should be parsed as UTC according to ECMA-262, which is contrary to ISO 8601 which would treat it as local), however that is not reliable in all implementations in use.
If you just want to get the current date in ISO 8601 format, you can do:
if (!Date.prototype.toISODate) { Date.prototype.toISODate = function() { return this.getFullYear() + '-' + ('0'+ (this.getMonth()+1)).slice(-2) + '-' + ('0'+ this.getDate()).slice(-2); } } console.log(new Date().toISODate());
However, since the built-in toISOString uses UTC this might be confusing. If the UTC date is required, then:
if (!Date.prototype.toUTCDate) { Date.prototype.toUTCDate = function() { return this.getUTCFullYear() + '-' + ('0'+ (this.getUTCMonth()+1)).slice(-2) + '-' + ('0'+ this.getUTCDate()).slice(-2); } } console.log(new Date().toUTCDate());
You can achieve this by simply formating everything that you need in place using moment.format()
and then simply append the extra Z to the string. You have to do this as Z within moment JS is reserved for outputting.
var date = moment('2014-08-28').format("YYYY-MM-DDT00:00:00.000") + "Z";
Fiddle example https://jsfiddle.net/2avxxz6q/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With