从ISO8601时间戳创建另一个格式的日期字符串 [英] Create another formated date string from an ISO8601 timestamp

查看:212
本文介绍了从ISO8601时间戳创建另一个格式的日期字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var date ='2014-02-02T20:10:00'; 
console.log(date);

为什么会返回以下内容?



< blockquote>

'2014-02-02T20:10:00'// ok


但是: p>

  var date = new Date(date); 
console.log(date);

返回:


Sun Feb 02 2014 21:10:00 GMT + 0100 // bad


我想收到以下内容,没有偏移:


Sun Feb 02 2014 20:10:00 GMT


如何做到这一点?

解决方案

你不能依靠 日期 正确解析一个字符串(这里是一个比较,那就是在引入ISO8601解析之前 - ECMA5)到一个 Date 对象,所以对于跨浏览器,最好自己做。您也不能依赖于从 Date.toString()返回的字符串,并且再次对于跨浏览器,您将需要自己格式化,或使用像 moments.js 。这很简单。



您所拥有的字符串是 ISO8601 格式,具体而言,根据ISO8601规范,假定为本地时间,因为没有提供偏移量,我将假定它是 UTC 。许多浏览器和图书馆,W3C和ECMA5规范(有更改假设的勘误)不同意,您不能认为当地时间是假定的。



你显然有一个支持这些字符串的浏览器。但是当您输出 Date .toString 您要求当地时间(根据您的环境),但您希望UTC(假设),因此您需要 Date.toUTCString() ,但是这些方法是依赖于实现的,您可能不会在不同的环境中获得相同的字符串。



Javascript

 函数parseMyDateString(str){
var parts = str.split(/ [\-T:] /);

parts [1] - = 1;

return new Date(Date.UTC.apply(undefined,parts));
}

函数padLeft(arg){
var str = String(arg);

if(str.length< 2){
str ='0'+ str;
}

return str;
}

函数formatMyDateString(date){
var days = [
'Sun',
'Mon',
'Tue' ,
'Wed',
'Thu',
'Fri',
'Sat'
],
months = [
' Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
' ,
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
],
dateString = [
days [date.getUTCDay()],
months [date.getUTCMonth()],
padLeft(date.getUTCDate()),
date.getUTCFullYear )
] .join(''),
timeString = [
padLeft(date.getUTCHours()),
padLeft(date.getUTC分钟()),
padLeft(date.getUTCSeconds())
] .join(':');

return [
dateString,
timeString,
'GMT'
] .join('');
}

var iso8601String ='2014-02-02T20:10:00',
dateObject = parseMyDateString(iso8601String),
myDateString = formatMyDateString(dateObject);

console.log(myDateString);

输出

  Sun Feb 02 2014 20:10:00 GMT 

jsFiddle


var date = '2014-02-02T20:10:00';
console.log(date);

Why does this return the following?:

'2014-02-02T20:10:00' //ok

but:

var date = new Date(date);
console.log(date);

returns:

Sun Feb 02 2014 21:10:00 GMT+0100 //bad

I would like to receive the following, where there is no offset:

Sun Feb 02 2014 20:10:00 GMT

How can I make it so?

解决方案

You can not rely on Date to parse a string correctly (here is a comparison and that's before the introduction of ISO8601 parsing - ECMA5) into a Date object, so for cross browser it is best to do it yourself. You also can not rely on the string returned from Date.toString() and again for cross browser you will need to format it yourself, or use a library like moments.js. It's that simple.

The string you have is a date stamp in ISO8601 format and specifically, by the ISO8601 specification, is assumed to be local time as no offset is supplied, I am going to assume it is UTC. Many browsers and libraries, the W3C and the ECMA5 spec (that have errata that change the assumption) disagree on this, and you can not take it for granted that local time is assumed.

You obviously have a browser that supports these strings. But when you output Date.toString you are asking for the local time (as per your environment), but you want UTC (assumed) and so you need Date.toUTCString(), but these methods are implementation dependant and you may not get the same string in different environments.

Javascript

function parseMyDateString(str) {
    var parts = str.split(/[\-T:]/);

    parts[1] -= 1;

    return new Date(Date.UTC.apply(undefined, parts));
}

function padLeft(arg) {
    var str = String(arg);

    if (str.length < 2) {
        str = '0' + str;
    }

    return str;
}

function formatMyDateString(date) {
    var days = [
            'Sun',
            'Mon',
            'Tue',
            'Wed',
            'Thu',
            'Fri',
            'Sat'
        ],
        months = [
            'Jan',
            'Feb',
            'Mar',
            'Apr',
            'May',
            'Jun',
            'Jul',
            'Aug',
            'Sep',
            'Oct',
            'Nov',
            'Dec'
        ],
        dateString = [
            days[date.getUTCDay()],
            months[date.getUTCMonth()],
            padLeft(date.getUTCDate()),
            date.getUTCFullYear()
        ].join(' '),
        timeString = [
            padLeft(date.getUTCHours()),
            padLeft(date.getUTCMinutes()),
            padLeft(date.getUTCSeconds())
        ].join(':');

    return [
            dateString,
            timeString,
            'GMT'
        ].join(' ');
}

var iso8601String = '2014-02-02T20:10:00',
    dateObject = parseMyDateString(iso8601String),
    myDateString = formatMyDateString(dateObject);

console.log(myDateString);

Output

Sun Feb 02 2014 20:10:00 GMT 

On jsFiddle

这篇关于从ISO8601时间戳创建另一个格式的日期字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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