7

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

Auberon Vacher
  • 4,655
  • 1
  • 24
  • 36

4 Answers4

14

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

Community
  • 1
  • 1
Auberon Vacher
  • 4,655
  • 1
  • 24
  • 36
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'
David Brockman
  • 567
  • 4
  • 6
1

I had a date "July 1, 2022" and a time "1:30 PM", and wanted it into the ical format, from a browser console.

const date = "July 1, 2022";
const time = "1:30 PM";

function calendarTimeFormat(date) {
    const year = Intl.DateTimeFormat('en', { year: 'numeric', timeZone: 'utc' }).format(date);
    const month = Intl.DateTimeFormat('en', { month: '2-digit', timeZone: 'utc' }).format(date);
    const day = Intl.DateTimeFormat('en', { day: '2-digit', timeZone: 'utc' }).format(date);
    const hour = Intl.DateTimeFormat('en', { hour: '2-digit', timeZone: 'utc', hour12: false }).format(date).split(' ')[0];
    const minute = Intl.DateTimeFormat('en', { minute: '2-digit', timeZone: 'utc' }).format(date).split(' ')[0];
    return year + month + day + "T" + hour + minute + "00Z";
}

const d = Date.parse(`${date} ${time}`);
console.log(calendarTimeFormat(d);
crackpotHouseplant
  • 380
  • 1
  • 3
  • 12
-3

So to answer the question to convert to this format in JQuery you can do the following.

var n = d.toISOString();
Geoff Wells
  • 361
  • 3
  • 10