Javascript从毫秒和时区的日期 [英] Javascript Date from milliseconds and timezone

查看:75
本文介绍了Javascript从毫秒和时区的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何格式化JSON日期?

从webservice解析日期

对不起,如果此问题已被询问。我环顾四周,但找不到一个。有没有一个快捷方便的方式来将 json 日期转换为仅使用 javascript jQuery的人性化格式(不包括额外的 jQuery 库)?

Sorry if this question has already been asked. I have look around but have been unable to find one. Is there a quick and convenient way to convert a "json" date into a human friendly format using only javascript and jQuery (excluding additional jQuerylibraries)?

日期格式如下:

creationDate: "/Date(1346713200000+0100)/"

谢谢

推荐答案

> var maybeDateString = "/Date(1346713200000+0100)/";
> fromDateString(maybeDateString)
Tue Sep 04 2012 02:00:00 GMT+0200

function fromDateString(str) {
    var res = str.match(/\/Date\((\d+)(?:([+-])(\d\d)(\d\d))?\)\//);
    if (res == null)
        return new Date(NaN); // or something that indicates it was not a DateString
    var time = parseInt(res[1], 10);
    if (res[2] && res[3] && res[4]) {
        var dir = res[2] == "+" ? -1 : 1,
            h = parseInt(res[3], 10),
            m = parseInt(res[4], 10);
        time += dir * (h*60+m) * 60000;
    }
    return new Date(time);
}

这篇关于Javascript从毫秒和时区的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆