Formatting time for ical export

13,349

Solution 1

Reading the RFC (link) gives :

3.3.5. Date-Time

[...]

date-time = date "T" time

The "DATE-TIME" value type expresses time values in three forms:

FORM #1: DATE WITH LOCAL TIME For example, the following represents January 18, 1998, at 11 PM: 19980118T230000

FORM #2: DATE WITH UTC TIME CAPITAL LETTER Z suffix character, to the time value.
For example, the following represents January 19, 1998, at 0700 UTC: 19980119T070000Z

FORM #3: DATE WITH LOCAL TIME AND TIME ZONE REFERENCE TZID=America/New_York:19980119T020000

DTSTART:19970714T133000 ; Local time DTSTART:19970714T173000Z ; UTC time DTSTART;TZID=America/New_York:19970714T133000 ; Local time and time ; zone reference

Solution 2

It's almost like toISOString

function formatDateTime(date) {
  const year = date.getUTCFullYear();
  const month = pad(date.getUTCMonth() + 1);
  const day = pad(date.getUTCDate());
  const hour = pad(date.getUTCHours());
  const minute = pad(date.getUTCMinutes());
  const second = pad(date.getUTCSeconds());
  return `${year}${month}${day}T${hour}${minute}${second}Z`;
}

function pad(i) {
  return i < 10 ? `0${i}` : `${i}`;
}

// Example:
const date = new Date('2017-05-31T11:46:54.216Z');
date.toISOString()   // '2017-05-31T11:46:54.216Z'
date.toJSON()        // '2017-05-31T11:46:54.216Z'
formatDateTime(date) // '20170531T114654Z'
Share:
13,349
Admin
Author by

Admin

Updated on June 29, 2022

Comments

  • Admin
    Admin almost 2 years

    I have created a calendar in jquery that exports to ical. However, I am having some issues with the datetime.

    The ical export script expects the date/time in this format: 19970714T170000Z.

    Does anybody know what this is and how I should prepare my string?

    Thanks