【问题标题】:Javascript Converting IETF Date to ISO8601 FormatJavascript 将 IETF 日期转换为 ISO8601 格式
【发布时间】:2011-03-24 03:40:58
【问题描述】:
【问题讨论】:
标签:
javascript
datetime
date
data-conversion
【解决方案1】:
来自fine manual:
date 保存当天的 Date 对象。
强调我的。
我猜您看到的 IETF 格式只是 Date 对象的默认字符串化。 Date class 具有 getYear()、getMonth() 和 getDate() 方法,因此如果您注意零填充,您可以轻松生成 ISO-8601 日期字符串。
【解决方案3】:
它返回一个常规的 javascript 日期对象。你可以像这样使用 fullcalendar formatDate 实用函数:
$.fullCalendar.formatDate(date, 'u');
full docs for formatDate
【解决方案4】:
您可以使用属于您的 Date 对象 dayClick 的 formatDate 函数!
function getGMTOffset(localDate) {
var positive = (localDate.getTimezoneOffset() > 0);
var aoff = Math.abs(localDate.getTimezoneOffset());
var hours = Math.floor(aoff / 60);
var mins = aoff % 60;
var offsetTz = padzero_(hours) + ':' + padzero_(mins);
// getTimezoneOffset() method returns difference between (GMT) and local time, in minutes.
// example, If your time zone is GMT+2, -120 will be returned.
// This is why we are inverting sign
if (!positive) {
return '+' + offsetTz;
}
return '-' + offsetTz;
}
function pad2zeros(n) {
if (n < 100) {
n = '0' + n;
}
if (n < 10) {
n = '0' + n;
}
return n;
}
function padzero(n) {
return n < 10 ? '0' + n : n.toString();
}
function formatDate(date) {
if (date) {
return (date.getFullYear()) +
'-' + padzero((date.getMonth() + 1)) +
'-' + padzero(date.getDate()) +
'T' + padzero(date.getHours()) +
':' + padzero(date.getMinutes()) +
':' + padzero(date.getSeconds()) +
'.' + pad2zeros(date.getMilliseconds()) +
getGMTOffset(date);
}
return '';
}