1

What is the date and time format like the following called, for example: 2001-07-26T20:52:54Z?

And how could I convert the date and time into that particular format and into string for JSON?

For example, if a user entered in 7/26/2001 and 20:52:54, I want it to be converted into 2001-07-26T20:52:54Z format.

Thank you

Reza S
  • 9,480
  • 3
  • 54
  • 84
Jo Ko
  • 7,225
  • 15
  • 62
  • 120

3 Answers3

5

Its called ISO 8601 data format.

Date:                          2016-08-19
Combined date and time in UTC: 2016-08-19T19:03:01+00:00
                               2016-08-19T19:03:01Z
                               20160819T190301Z
Week:                          2016-W33
Date with week number:         2016-W33-5
Date without year:             --08-19
Ordinal date:                  2016-232

For conversion, you could use Date#toISOString

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".

var today = new Date('05 October 2011 14:48 UTC');
console.log(today.toISOString()); // Returns 2011-10-05T14:48:00.000Z123

Example

function convert() {
    var date = document.forms.iso.date.value,
        time = document.forms.iso.time.value,
        dateTime = new Date(date + ' ' + time + ' UTC');
    document.getElementById('iso').innerHTML = dateTime.toISOString();
}
<form name="iso">
    date: <input type="text" name="date" value="7/26/2001"/><br />
    time: <input type="text"name="time" value="20:52:54" /><br />
    <button onclick="convert();">Convert</button>
</form>
<div id="iso"></div>
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Thank you for the in-depth info! Do you mind showing what and how the user should be inputting, using ``, to be passed properly and formatted into the ISO 8601 format? – Jo Ko Aug 19 '16 at 20:43
  • @JoKo, please see edit. don't forget to put the `UTC` to the string, because if not, you input a local time and get the a different zulu time. – Nina Scholz Aug 19 '16 at 21:15
  • sorry but in exactly what format is the user supposed to enter in the information? For example, for the date, they can't just manually put in 10/5/2001. – Jo Ko Aug 22 '16 at 18:31
  • it's just like the example values. – Nina Scholz Aug 22 '16 at 20:59
  • sorry but is that supposed to be the placeholder? – Jo Ko Aug 23 '16 at 16:55
  • i do not understand the question. – Nina Scholz Aug 23 '16 at 17:02
  • so if a user were to enter `7/20/2005` for date and `20:52:20` for time into the inputs, it will automatically translate it and reformat itself to `20 July 2005 20:52 UTC`? – Jo Ko Aug 23 '16 at 18:45
  • yes, that`s right. – Nina Scholz Aug 23 '16 at 20:39
  • Sorry forgot to accept the answer and upvote. Before I do, one more quick question, I got it to work but after following: `var newDateTime = new Date(startDate + startTime + ' UTC');` and `var timeStart = newDateTime.toISOString();`, and console logged `timeStart` and got `2001-05-20T20:11:22.000Z` but would like to get rid of the `.000`, so like `2001-05-20T20:11:22Z`. How can I do so? Thank you again – Jo Ko Sep 15 '16 at 21:52
  • you could replace the dot and last zeroes with an empty string, like `timeStart = timeStart.replace('.000', '');`. – Nina Scholz Sep 16 '16 at 05:51
  • But that would only apply in the case of `.000`, correct? Is there a way that applies in all cases? Just to leave out the milliseconds. – Jo Ko Sep 19 '16 at 20:18
  • then use a regular expression like `timeStart = timeStart.replace(/\.\d{3}/, '');`. it looks for the dot an thre digits. – Nina Scholz Sep 19 '16 at 20:32
1

You can visit this page. As long as you know what that standard is called and google you should be able to figure out everything else.

var d = new Date();
var n = d.toISOString();
n.replace(/\.\d{3}/,"")

2016-09-16T06:38:12Z
Reza S
  • 9,480
  • 3
  • 54
  • 84
  • "As long as you know what that standard is called" I'm pretty sure that's what OP was asking. Also, [w3schools is not the best resource](http://www.w3fools.com/) for reference. Decent for beginner information but MDN is a much better reference for particular functions. – Mike Cluck Aug 19 '16 at 20:14
  • @RezaS but before I accept the answer and upvote, how can I get rid of the `.376` that follows after? So would just want `2016-08-19T20:11:39Z` – Jo Ko Sep 15 '16 at 21:54
  • @JoKo You can do that using a regular expression, or follow the answer here: http://stackoverflow.com/a/8563517/196917 and leave out the milliseconds part – Reza S Sep 16 '16 at 00:38
  • @RezaS I tried but didn't work my end. To confirm could you show so I can accept the answer and upvote as well? – Jo Ko Sep 16 '16 at 00:53
  • 1
    updated answer... – Reza S Sep 16 '16 at 06:38
  • @RezaS thank you works fine! – Jo Ko Sep 19 '16 at 20:49
0

It's called ISO 8601 format. If you're using Python simply call isoformat() method, for other languages see following links: C#, Java, JavaScript. Anyway google is really helpful there.

Community
  • 1
  • 1
Yakuza
  • 3,237
  • 2
  • 21
  • 18