1

我有一个格式为:的变量集yyyy-mm-dd,我想将其转换为Tuesday 25th使用 JS 的格式(jQuery 库。)

我试过了:

var now = new Date('2013-06-25').format("l jS");
4

3 回答 3

3

Moment js是一个很棒的 javascript 库,用于处理 js 的日期对象,并且包括格式化日期对象的灵活方法。

这将为您提供您正在寻找的格式。

moment().format('dddd Do');

注意:时刻对象是本机日期对象的包装器。

于 2013-06-25T13:53:37.907 回答
1

问题1:

jQuery 是一个 DOM 操作库,因此不会对日期做任何事情。您要么必须使用另一个库,要么编写自己的 JavaScript。

问题2:

Date() 函数/构造函数在某些浏览器中无法识别该格式,因此您必须自己解析它:

var s = '2013-06-25',
    y = +s.substr(0, 4),     // get the year
    m = +s.substr(5, 2) - 1, // get the month
    d = +s.substr(8, 2),     // get the date of the month
    date = new Date(y, m, d);

问题3:

JavaScript 中没有自定义日期格式。此外,没有办法获得日期名称。

var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
            'Friday', 'Saturday'];
var formatted = days[date.getDay()] + ' ' + d;

问题4:

没有办法添加'th','nd'等......

if (Math.floor(d % 100 / 10) === 1) { // add 'th' for the 11th, 12th, and 13th
  formatted += 'th';
}
else {
  formatted += {1: 'st', 2: 'nd', 3: 'rd'}[d % 10] || 'th';
}
于 2013-06-25T14:05:08.257 回答
1

您可以使用 jQuery dateFormat 插件。你有很多可能性:

https://github.com/phstc/jquery-dateFormat

于 2013-06-25T13:48:33.767 回答