[javascript] Convert an ISO date to the date format yyyy-mm-dd in JavaScript

How can I get a date having the format yyyy-mm-dd from an ISO 8601 date?

My  8601 date is

2013-03-10T02:00:00Z

How can I get the following?

2013-03-10

This question is related to javascript date

The answer is


_x000D_
_x000D_
let isoDate = "2013-03-10T02:00:00Z";_x000D_
var d = new Date(isoDate);_x000D_
d.toLocaleDateString('en-GB'); // dd/mm/yyyy_x000D_
d.toLocaleDateString('en-US'); // mm/dd/yyyy
_x000D_
_x000D_
_x000D_


Moment.js will handle date formatting for you. Here is how to include it via a JavaScript tag, and then an example of how to use Moment.js to format a date.

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
moment("2013-03-10T02:00:00Z").format("YYYY-MM-DD") // "2013-03-10"

This is what I do to get date only:

_x000D_
_x000D_
let isoDate = "2013-03-10T02:00:00Z";_x000D_
_x000D_
alert(isoDate.split("T")[0]);
_x000D_
_x000D_
_x000D_


Use:

new Date().toISOString().substring(0, 10);

Pass your date in the date object:

var d = new Date('2013-03-10T02:00:00Z');
d.toLocaleDateString().replace(/\//g, '-');

A better version of answer by @Hozefa.

If you have date-fns installed, you could use formatISO function

const date = new Date(2019, 0, 2)
import { formatISO } from 'date-fns'
formatISO(date, { representation: 'date' }) // '2019-01-02' string

If you have a date object:

_x000D_
_x000D_
let date = new Date()_x000D_
let result = date.toISOString().split`T`[0]_x000D_
_x000D_
console.log(result)
_x000D_
_x000D_
_x000D_

or

_x000D_
_x000D_
let date = new Date()_x000D_
let result = date.toISOString().slice(0, 10)_x000D_
_x000D_
console.log(result)
_x000D_
_x000D_
_x000D_


Use the below code. It is useful for you.

let currentDate = new Date()
currentDate.toISOString()

To extend on rk rk's solution: In case you want the format to include the time, you can add the toTimeString() to your string, and then strip the GMT part, as follows:

var d = new Date('2013-03-10T02:00:00Z');
var fd = d.toLocaleDateString() + ' ' + d.toTimeString().substring(0, d.toTimeString().indexOf("GMT"));

let dt = new Date('2013-03-10T02:00:00Z');
let dd = dt.getDate();
let mm = dt.getMonth() + 1;
let yyyy = dt.getFullYear();

if (dd<10) {
    dd = '0' + dd;
}
if (mm<10) {
    mm = '0' + mm;
}
return yyyy + '-' + mm + '-' + dd;

Just crop the string:

var date = new Date("2013-03-10T02:00:00Z");
date.toISOString().substring(0, 10);

Or if you need only date out of string.

var strDate = "2013-03-10T02:00:00Z";
strDate.substring(0, 10);

This will output the date in YYYY-MM-DD format:

let date = new Date();
date = date.toISOString().slice(0,10);

You could checkout Moment.js, Luxon, date-fns or Day.js for nice date manipulation.

Or just extract the first part of your ISO string, it already contains what you want. Here is an example by splitting on the T:

"2013-03-10T02:00:00Z".split("T")[0] // "2013-03-10"

Moment.js is pretty big library to use for a single use case. I recommend using date-fns instead. It offers basically the most functionality of Moment.js with a much smaller bundle size and many formatting options.

import format from 'date-fns/format'
format('2013-03-10T02:00:00Z', 'YYYY-MM-DD'); // 2013-03-10, YYYY-MM-dd for 2.x

One thing to note is that, since it's the ISO 8601 time format, the browser generally converts from UTC time to local timezone. Though this is simple use case where you can probably do '2013-03-10T02:00:00Z'.substring(0, 10);.

For more complex conversions date-fns is the way to go.


I used this:

HTMLDatetoIsoDate(htmlDate){
  let year = Number(htmlDate.toString().substring(0, 4))
  let month = Number(htmlDate.toString().substring(5, 7))
  let day = Number(htmlDate.toString().substring(8, 10))
  return new Date(year, month - 1, day)
}

isoDateToHtmlDate(isoDate){
  let date = new Date(isoDate);
  let dtString = ''
  let monthString = ''
  if (date.getDate() < 10) {
    dtString = '0' + date.getDate();
  } else {
    dtString = String(date.getDate())
  }
  if (date.getMonth()+1 < 10) {
    monthString = '0' + Number(date.getMonth()+1);
  } else {
    monthString = String(date.getMonth()+1);
  }
  return date.getFullYear()+'-' + monthString + '-'+dtString
}

Source: http://gooplus.fr/en/2017/07/13/angular2-typescript-isodate-to-html-date/


_x000D_
_x000D_
    var d = new Date("Wed Mar 25 2015 05:30:00 GMT+0530 (India Standard Time)");_x000D_
    alert(d.toLocaleDateString());
_x000D_
_x000D_
_x000D_


To all who are using split, slice and other string-based attempts to obtain the date, you might set yourself up for timezone related fails!

An ISO-String has Zulu-Timezone and a date according to this timezone, which means, it might use a date a day prior or later to the actual timezone, which you have to take into account in your transformation chain.

See this example:

const timeZoneRelatedDate = new Date(2020, 0, 14, 0, 0);

console.log(timeZoneRelatedDate.toLocaleDateString(
    'ja-JP', 
    {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit'
    }
).replace(/\//gi,'-'));

// RESULT: "2020-01-14"

console.log(timeZoneRelatedDate.toISOString());

// RESULT: "2020-01-13T23:00:00.000Z" (for me in UTC+1)

console.log(timeZoneRelatedDate.toISOString().slice(0,10));

// RESULT: "2020-01-13"