needs understanding of this format of date and time - javascript

I just came across this format of start and end in the codebase which is probably the starting and ending date and time.However, I could not figure out this.
I know how to generate date and time in javascript but I need to generate it in this format to be able to fetch data from API.
Here is the format.
start : '2018-06-20T11:44:21.938Z',
end : '2018-07-20T11:44:21.938Z',
At the same time, I would like to know how to get date and time with exact this format?

It is ISO format of the date. You can use toISOString() method to achieve it:
// current date
var date = new Date();
console.log(date);
console.log(date.toISOString());

let date = new Date( Date.parse('2018-06-20T11:44:21.938Z'));
To retrieve date & time separately.
date.toLocaleDateString();
date.toLocaleTimeString()'
To convert any date object to your desired format (ISO Dates):
var date = new Date();
date.toISOString();

var date = new Date(Date.parse('2018-06-20T11:44:21.938Z'));
It's ISO 8601 format.
Check this:
https://en.wikipedia.org/wiki/ISO_8601

Have a look at toISOString() in MDN
var event = new Date('05 October 2011 14:48 UTC');
console.log(event.toString());
// expected output: Wed Oct 05 2011 16:48:00 GMT+0200 (CEST)
// (note: your timezone may vary)
console.log(event.toISOString());
// expected output: 2011-10-05T14:48:00.000Z

That is the ISO date format.
similar to this:
How do I output an ISO 8601 formatted string in JavaScript?
Here is code to produce it.
function myFunction() {
var d = new Date();
var n = d.toISOString();
document.getElementById("demo").innerHTML = n;
}
<button onclick="myFunction()">convert to ISO date format</button>
<p id="demo"></p>

It is an ISO date. Check this link for some info:
https://www.w3schools.com/js/js_date_formats.asp
var istDate = new Date(); // will return: Wed Sep 05 2018 17:50:39 GMT+0530 (India Standard Time)
var isoDate = istDate.toISOString(); // will return in ISO format i.e. "2018-09-05T12:20:39.732Z"
var dateString = istDate.toDateString(); // will return in format: "Wed Sep 05 2018"
Browser console will provide suggestions for various methods of Date Object for extracting day, month, year, etc from the new Date()

Related

How to convert a german date string into a date correctly?

I have a string, which I want to convert to a Date;
let dateStr = "01.04.1990"
let date = new Date(dateStr);
but if I try to console log the date I get Thu Jan 04 1990 00:00:00. As you see day and month are switched but why?
How would I convert that string correctly?
You could reorder the values for an ISO date string and get the instance with this value.
let dateStr = "01.04.1990"
let date = new Date(dateStr.replace(/(.*)\.(.*)\.(.*)/, '$3-$2-$1'));
console.log(date);
In genereal Date.parse() is expecting an ISO-8601 formatted date string.
A recommendable approach would be to use a library like Luxon, as suggested here: stackoverflow

How to convert a string in yyyy-mm-dd format to a Javascript Date Object without considering the date as UTC

I really just have a simple question. I want to convert a string in yyyy-mm-dd to a date object in javascript. This seems simple enough. You can just do the following.
var date = new Date('2020-04-08');
But in this case, javascript considers the date '2020-04-08' as UTC midnight. So the actual date that is returned to me is the following.
Tue Apr 07 2020 17:00:00 GMT-0700 (Pacific Daylight Time)
I know I can create a date in the following format
var date = new Date(2020, 3, 8);
Wed Apr 08 2020 00:00:00 GMT-0700 (Pacific Daylight Time)
But it looks like too much hassle to extract the year, month and date from the already nicely formatted date string. Isn't there a simple way to convert yyyy-mm-dd string to a date object without it being treated as UTC date?
So in the simplest terms, my question is how do I tell the date constructor that the date I am passing is not a UTC date but a local date.
Simply append 'T00:00:00' to your date string.
const dateString = '2020-04-08'
const timeString = 'T00:00:00'
const date = new Date(dateString + timeString);
console.info(date.toString())
From the documentation
Support for ISO 8601 formats differs in that date-only strings (e.g. "1970-01-01") are treated as UTC, not local.

Creating a Date is adding automatically one hour to my input date

let's say I have this date as input :
var _dateA = 2018-11-15T11:13:26.687Z
If I'm doing, whatever,
var _dateB = new Date(_date)
or
var _dateB = moment(_date)
I get this as result ==>
_dateB = Thu Nov 15 2018 12:13:26 GMT+0100 (heure normale d’Europe centrale)
I understood that there's timezone trouble, but how can I get a Date object or Moment object, without having this one hour more?
Wanted result => Thu Nov 15 2018 11:13:26 GMT+0100
Current result => Thu Nov 15 2018 12:13:26 GMT+0100
You need to use Date.toUTCString() that converts date to string, using the UTC time zone
var _dateA = '2018-11-15T11:13:26.687Z';
var _dateB = new Date(_dateA);
console.log(_dateB.toUTCString());
When you "output" a Date object via console.log(), alert(), etc the toString() method is used by default, converting the date object to a local timezone string for display (which is why you are seeing your date in your local time).
Parsing date strings with the Date constructor is not recommended (although, I suspect that most browsers probably handle ISO 8601 dates, like the one in your question, fairly well) - see the dateString parameter note here. So, if you need to construct a date object as well as output a date string, then you could parse the ISO 8601 string with split() using a regex character set for multiple delimiters, then construct a UTC date object with new Date(Date.UTC(...)). You could also do this with moment.js but the below should illustrate what is happening in a bit more detail.
For example:
const text = '2018-11-15T11:13:26.687Z'
const [y, m, d, hh, mm, ss, ms] = text.split(/[-T:.Z]/);
const date = new Date(Date.UTC(y, m - 1, d, hh, mm, ss, ms));
console.log(date.toLocaleString()); // date string in local timezone
console.log(date.toUTCString()); // UTC date string
console.log(JSON.stringify(date)); // ISO 8601 date string in most browsers

Javascript date to xs:dateTime

Is there a simple way to convert standard Javascript date format to xs:dateTime
So I have a date value (new Date()) and I need in the format: 2015-01-16T20:26:53.974+03:00
so
Fri Jan 16 2015 22:26:53 GMT+0500 (Ekaterinburg Standard Time) ->
2015-01-16T20:26:53.974+03:00
It strange but cound't find simple solution.
I believe this is the same as the ISO date format
var date = new Date();
var formatted = date.toISOString();

Converting Javascript Date

I have a date String in this format: Tue Sep 02 00:00:00 GMT+200 2014, I'd like to have only in Javascript this ISO Format: 2014-09-02T00:00:00.000Z.
So I have wrote this code:
var date = new Date("Tue Sep 02 00:00:00 GMT+200 2014");
date.toJSON();
but it returns: "2014-09-01T22:00:00.000Z".
How can I have the right date in ISO format? Thank you.
If you want to use the JavaScript native Date Object, you may want to look at its documentation first, especially the toISOString() method.
var date = new Date("Tue Sep 02 00:00:00 GMT+200 2014");
var n = date.toISOString();
this returns:
n: '2014-09-01T22:00:00.000Z'
which is the right ISO format. Your initial time is GMT+2 so, in ISO time, it corresponds to the same date/time but two hours before. As it is the 2nd of Sept, 00:00:00, 2 hours before lead to the day before, the 1st of Sept, at 22:00:00. You can't have the 2014-09-02T00:00:00.000Z you want in your question because it is not corresponding to an ISO date.
You can read more about ISO 8601 on Wikipedia.
There is a method for that.
date.toISOString()

Categories