3
 function tolocal(str)
 {
 var date, split, dSplit, tSplit, d, raw;
date = '';
split = str.split(' ');
if (split.length === 2) {
    dSplit = split[0].split('-');
    tSplit = split[1].split(':');
}
raw = d.toLocaleString().split(' GMT')[0];

 return raw.substring(raw.indexOf(", ")+2, raw.lastIndexOf(':')) + " " + raw.substring(raw.length-2,raw.length)
 }

上面的代码在 ie 浏览器中运行良好,我得到以下格式的输出。

2012-11-13 10:15

但我无法在 chrome 浏览器中实现相同的功能。还有其他功能可以帮助我实现相同的输出吗? date.toUTCString()提供相同的结果,但我不确定它toLocaleString()在功能方面有何不同。

提前致谢。

4

3 回答 3

3

也许你可以使用第三方库来做这样的事情:moment.js是一个很好的。例子:

moment(d).format('MMMM Do, YYYY h:mms a');
于 2013-10-15T11:15:38.570 回答
3

只需手动执行:

// Where "date" is a Date object
function dateFormatUTC(date) {
  var months = [
    'January', 'February', 'March', 'April', 'May', 'June',
    'July', 'August', 'September', 'October', 'November', 'December'
  ];

  var hours = date.getUTCHours();
  if (hours < 10) hours = '0' + hours;

  var minutes = date.getUTCMinutes();
  if (hours < 10) hours = '0' + hours;

  var monthName = months[date.getUTCMonth()];
  var timeOfDay = hours < 12 ? 'AM' : 'PM';

  return monthName + ' ' + date.getUTCDate() + ', ' +
         date.getUTCFullYear() + ' ' + hours + ':' + minutes + timeOfDay;
}
于 2013-10-16T01:07:49.357 回答
1

您可以尝试使用以下选项:

  var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
          // request a weekday along with a long date
   var options = {weekday: "long", year: "numeric", month: "long", day: "numeric"};
     // an application may want to use UTC and make that visible
    options.timeZone = "UTC";
    options.timeZoneName = "short";
    alert(date.toLocaleString("en-US", options));

请找到参考@

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString

于 2013-10-15T12:08:47.153 回答