2

在 js 中使用日期时间时,我并不是特别熟练。我有一个 JSON 对象,其中有一些日期时间字段的格式,就像YYYY-MM-DDTHH:MM:SS现在我必须把它放进去HH:mm AM/PM,我想出了:

var d = Date.parse('2013-04-17T13:05:00');
var date = new Date(d);
var hrs = date.getUTCHours();
var min = date.getUTCMinutes();
var meridian;
if(hrs > 12){
    hrs -= 12;
    meridian = 'PM';
}
else {
    meridian = 'AM';
}
if (min < 10) {
    min = "0"+min;
};
var time = hrs+":"+min+" "+meridian;

有没有更好的方法来实现这一点?

4

3 回答 3

8

试试 moment.js,这个库内置了大量的功能。

http://momentjs.com/

moment(String, String);
moment("12-25-1995", "MM-DD-YYYY");

“hA”是“3PM”格式的正确格式

于 2013-06-18T19:26:18.047 回答
0

不同方法的不同答案

有一些像现在这样格式化的日期时间字段YYYY-MM-DDTHH:MM:SS我必须把它放进去HH:mm AM/PM

正如您所知道的格式,您可以直接从 datetime String中获取您想要的部分,避免一起使用Date实例或库。

var stamp = '2013-04-17T13:05:00',
    hrs = +stamp.slice(11, 13), // 13
    min = +stamp.slice(14, 16); //  5

然后从这里像以前一样继续。

于 2013-06-18T22:51:36.247 回答
-1

这有点矫枉过正,但我​​最近特意为这样的事情制作了自己的功能

var easyDate = (function () {
    var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
        months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
        thstndrd = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th'],
        toDate = function () {return new Date(Date.UTC(this.yyyy, this.MM - 1, this.dd, this.hh, this.mm, this.ss, this.ms));},
        toLocaleDate = function () {return new Date(this.yyyy, this.MM - 1, this.dd, this.hh, this.mm, this.ss, this.ms);},
        UTC = function UTC(d) {
            return {
                ms: d.getUTCMilliseconds(),
                ss: d.getUTCSeconds(),
                mm: d.getUTCMinutes(),
                hh: d.getUTCHours(),
                dd: d.getUTCDate(),
                MM: d.getUTCMonth() + 1,
                yyyy: d.getUTCFullYear(),
                dow: d.getUTCDay(),
                toDate: toDate,
                toLocaleDate: toLocaleDate
            }
        },
        easyDate = function easyDate(d) {
            var o = UTC(d);
            return {
                dd: '' + o.dd,
                th: thstndrd[o.dd % 10],
                day: days[o.dow],
                MM: '' + o.MM,
                month: months[o.MM - 1],
                year: '' + o.yyyy,
                am: o.hh < 12 ? 'a.m.' : 'p.m.',
                hh: o.hh < 10 ? '0' + o.hh : '' + o.hh,
                h: '' + (o.hh % 12 || 12),
                mm: o.mm < 10 ? '0' + o.mm : '' + o.mm,
                ss: o.ss < 10 ? '0' + o.ss : '' + o.ss,
                ms: ('00' + o.ms).slice(-3),
                UTC: o
            };
        };
    easyDate.UTC = UTC;
    easyDate.delta = function (then, now) {
        var o, p, dir = -1;
        now || (now = new Date());
        if (now >= then) o = UTC(now), p = UTC(then);
        else o = UTC(then), p = UTC(now), now = then, dir = 1;
        o.dir = dir;
        o.dow = p.dow;
        o.ms = o.ms - p.ms;
        o.ss = o.ss - p.ss;
        o.mm = o.mm - p.mm;
        o.hh = o.hh - p.hh;
        o.dd = o.dd - p.dd;
        o.MM = o.MM - p.MM;
        o.yyyy = o.yyyy - p.yyyy;
        // fix negatives
        if (o.ms < 0) --o.ss, o.ms = (o.ms + 1000) % 1000;
        if (o.ss < 0) --o.mm, o.ss = (o.ss +   60) %   60;
        if (o.mm < 0) --o.hh, o.mm = (o.mm +   60) %   60;
        if (o.hh < 0) --o.dd, o.hh = (o.hh +   24) %   24;
        if (o.dd < 0) { // months have different lengths
            --o.MM;
            now.setUTCMonth(now.getUTCMonth() + 1);
            now.setUTCDate(0);
            o.dd = (o.dd + now.getUTCDate()) % now.getUTCDate();
        }
        if (o.MM < 0)  --o.yyyy, o.MM = (o.MM + 12) % 12;
        return o;
    };
    return easyDate;
}());

并使用它

var o = easyDate(new Date());
o.month + ' ' + o.dd + o.th + ', ' + o.h + ':' + o.mm + ' ' + o.am;
// "June 18th, 7:30 p.m."

如果您注意到easyDate.delta代码中调用的东西,是的,它也支持计算时间差异。如果您只想getUTC*为所有事情而奔跑,那么您拥有easyDate.UTC. :)

于 2013-06-18T19:30:44.380 回答