Formatting time for ical export

随声附和 提交于 2019-12-08 17:34:22

问题


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


回答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




回答2:


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

var n = d.toISOString();



回答3:


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'


来源:https://stackoverflow.com/questions/10518804/formatting-time-for-ical-export

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!