Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Javascript中从字符串获取时区偏移量_Javascript_Timezone - Fatal编程技术网

如何在Javascript中从字符串获取时区偏移量

如何在Javascript中从字符串获取时区偏移量,javascript,timezone,Javascript,Timezone,我收到一个带有偏移量的字符串格式的日期,但javascript正在将其转换为本地设备时间 var d = new Date("2012-11-13T11:34:58-05:00"); debug.log(d); 返回时间:2012年11月13日星期二17:34:58 GMT+0100(CET) var offset = d.getTimezoneOffset(); debug.log(offset); 返回-60(我的设备是utc+1h) 我只想得到带有偏移量的时间,或者字符串中提到的时区偏

我收到一个带有偏移量的字符串格式的日期,但javascript正在将其转换为本地设备时间

var d = new Date("2012-11-13T11:34:58-05:00");
debug.log(d);
返回时间:2012年11月13日星期二17:34:58 GMT+0100(CET)

var offset = d.getTimezoneOffset();
debug.log(offset);
返回-60(我的设备是utc+1h)


我只想得到带有偏移量的时间,或者字符串中提到的时区偏移量(-示例中为5h)

我不完全确定我是否知道您的要求,但我不认为javascript可以提供本地偏移量以外的偏移量,因为它不知道您给出的时间来自何处


所以你给了它“2012-11-13T11:34:58-05:00”,但没有关于这个时间起源的信息,它可能是世界上任何地方的时间,所以它默认为本地时区,如果是偏移量。

我找到的唯一解决方案是通过解析字符串来创建我的自定义时间对象

//ex: 2012-11-13T10:56:58-05:00
function CustomDate(timeString){

    var completeDate = timeString.split("T")[0];
    var timeAndOffset = timeString.split("T")[1];
    //date
    this.year = completeDate.split("-")[0];
    this.month = completeDate.split("-")[1];
    this.day = completeDate.split("-")[2];

    this.date = this.year + "/" + this.month + "/"+this.day;

    //negative time offset
    if (timeAndOffset.search("-") != -1){
        var completeOffset = timeAndOffset.split("-")[1];
        this.offset = parseInt(completeOffset.split(":")[0]) * -1;

        var originalTime = timeAndOffset.split("-")[0];
        this.hours = parseInt(originalTime.split(":")[0]);
        this.minutes = parseInt(originalTime.split(":")[1]);
        this.seconds = parseInt(originalTime.split(":")[2]);

        this.time = this.hours + ":" + this.minutes + ":"+this.seconds;

    }
    ///positive time offset
    else if (timeAndOffset.search(/\+/) != -1){

        var completeOffset = timeAndOffset.split("+")[1];
        this.offset = parseInt(completeOffset.split(":")[0]);

        var originalTime = timeAndOffset.split("+")[0];
        this.hours = parseInt( originalTime.split(":")[0]);
        this.minutes = parseInt(originalTime.split(":")[1]);
        this.seconds = parseInt(originalTime.split(":")[2]);

        this.time = this.hours + ":" + this.minutes + ":"+this.seconds;
    }
    //no time offset declared
    else{
        this.hours = parseInt(timeAndOffset.split(":")[0]);
        this.minutes = parseInt(timeAndOffset.split(":")[1]);
        this.seconds = parseInt(timeAndOffset.split(":")[2]);
        this.offset = 0;

        this.time = this.hours + ":" + this.minutes + ":"+this.seconds;
    }
}
例如,如果我想在指定的时区偏移中显示接收到的时间2012-11-13T11:34:58-05:00:

var aDate = new CustomDate("2012-11-13T11:34:58-05:00");
alert("date: " + aDate.date +" time: "+aDate.time+" offset: "+aDate.offset);
我得到

date: 2012/11/13 time: 11:34:58 offset: -5

此解决方案的问题在于,日期和时间约定是在代码中手动定义的,因此它们不会自动适应用户的语言。

我认为是时间结束时的负5(-05:00)表示示例中的时区偏移量,我说的是纽约时间上午11:34。好吧,你可能是对的,我不确定。你知道-60就是-60分钟,对吗?所以我想你可以把它转换回hours-60/1=-1,然后将它添加到字符串中。是的,我知道-60是-60分钟,但那太晚了,这表示设备时间偏移,而不是字符串的偏移。问题是,我从服务器接收到的字符串的偏移量可能不同于字符串之间的偏移量,我只想读取字符串中指定时区的时间。这样,如果我还想在提取偏移量后获得时区缩写(IST,EST),该怎么办。有这方面的图书馆吗?