我有一个Date对象。如何呈现以下代码片段的标题部分?

<abbr title="2010-04-02T14:12:07">A couple days ago</abbr>

我有另一个库的“相对时间”部分。

我试过以下几种方法:

function isoDate(msSinceEpoch) {

   var d = new Date(msSinceEpoch);
   return d.getUTCFullYear() + '-' + (d.getUTCMonth() + 1) + '-' + d.getUTCDate() + 'T' +
          d.getUTCHours() + ':' + d.getUTCMinutes() + ':' + d.getUTCSeconds();

}

但这给了我:

"2010-4-2T3:19"

当前回答

toISOString的问题是它只将datetime作为“Z”。

ISO-8601还定义了带小时和分钟时区差异的datetime,格式为2016-07-16T19:20:30+5:30(当时区在UTC之前时)和2016-07-16T19:20:30-01:00(当时区在UTC之后时)。

我不认为使用另一个插件moment.js来完成这么小的任务是个好主意,尤其是当你可以用几行代码就得到它的时候。

一旦有了以小时和分钟为单位的时区偏移量,就可以追加到datetime字符串。

我为此写了一篇博客:http://usefulangle.com/post/30/javascript-get-date-time-with-offset-hours-minutes

var timezone_offset_min = new Date().getTimezoneOffset(), offset_hrs = parseInt(Math.abs(timezone_offset_min / 60)), offset_min = Math.abs(timezone_offset_min % 60), timezone_standard; if (offset_hrs < 10) offset_hrs = '0' + offset_hrs; if (offset_min > 10) offset_min = '0' + offset_min; // getTimezoneOffset returns an offset which is positive if the local timezone is behind UTC and vice-versa. // So add an opposite sign to the offset // If offset is 0, it means timezone is UTC if (timezone_offset_min < 0) timezone_standard = '+' + offset_hrs + ':' + offset_min; else if (timezone_offset_min > 0) timezone_standard = '-' + offset_hrs + ':' + offset_min; else if (timezone_offset_min == 0) timezone_standard = 'Z'; // Timezone difference in hours and minutes // String such as +5:30 or -6:00 or Z console.log(timezone_standard);

其他回答

我可以用非常少的代码得到以下的输出。

var ps = new Date('2010-04-02T14:12:07')  ;
ps = ps.toDateString() + " " + ps.getHours() + ":"+ ps.getMinutes() + " hrs";

输出:

Fri Apr 02 2010 19:42 hrs

最短,但ie8及更早版本不支持:

new Date().toJSON()

已经有了一个叫toISOString()的函数:

var date = new Date();
date.toISOString(); //"2011-12-19T15:28:46.493Z"

如果,不知怎的,你在一个不支持它的浏览器上,我已经为你提供了:

if (!Date.prototype.toISOString) { (function() { function pad(number) { var r = String(number); if (r.length === 1) { r = '0' + r; } return r; } Date.prototype.toISOString = function() { return this.getUTCFullYear() + '-' + pad(this.getUTCMonth() + 1) + '-' + pad(this.getUTCDate()) + 'T' + pad(this.getUTCHours()) + ':' + pad(this.getUTCMinutes()) + ':' + pad(this.getUTCSeconds()) + '.' + String((this.getUTCMilliseconds() / 1000).toFixed(3)).slice(2, 5) + 'Z'; }; }()); } console.log(new Date().toISOString())

用一些糖和现代语法来扩展肖恩伟大而简洁的回答:

// date.js

const getMonthName = (num) => {
  const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Oct', 'Nov', 'Dec'];
  return months[num];
};

const formatDate = (d) => {
  const date = new Date(d);
  const year = date.getFullYear();
  const month = getMonthName(date.getMonth());
  const day = ('0' + date.getDate()).slice(-2);
  const hour = ('0' + date.getHours()).slice(-2);
  const minutes = ('0' + date.getMinutes()).slice(-2);

  return `${year} ${month} ${day}, ${hour}:${minutes}`;
};

module.exports = formatDate;

然后如。

import formatDate = require('./date');

const myDate = "2018-07-24T13:44:46.493Z"; // Actual value from wherever, eg. MongoDB date
console.log(formatDate(myDate)); // 2018 Jul 24, 13:44

我想我找到了一个更好的解决方案:

根据维基页面,加拿大使用ISO 8601作为官方日期格式,因此我们可以安全地使用它。

console.log(new Date("2022-12-19 00:43:00 GMT+0100").toISOString().split("T")[0]); // result in '2022-12-18' console.log(new Date("2022-12-19 00:43:00 GMT+0100").toLocaleDateString("en-CA")); // result in '2022-12-19'