0

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?

Hyyan Abo Fakher
  • 3,497
  • 3
  • 21
  • 35
jammy
  • 857
  • 2
  • 18
  • 34

6 Answers6

1
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();
shreyas d
  • 774
  • 1
  • 4
  • 16
  • Can you tell me how to generate date exact this way?Y es, your answer helped to understand. – jammy Sep 05 '18 at 12:16
1
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

brooksrelyt
  • 3,925
  • 5
  • 31
  • 54
lviggiani
  • 5,824
  • 12
  • 56
  • 89
1

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());
Rohit Sharma
  • 3,304
  • 2
  • 19
  • 34
0

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
ibex
  • 1,038
  • 9
  • 18
0

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>
jeffld
  • 726
  • 1
  • 9
  • 17
0

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()

brooksrelyt
  • 3,925
  • 5
  • 31
  • 54
Aayush Kumar
  • 37
  • 1
  • 8