没有时区的Javascript日期对象

时间:2016-10-11 08:18:37

标签: javascript html cordova date datetime

我最近为基于云的系统实施了一个基于webview的移动应用程序,而且我遇到了一个令人沮丧的问题。

我们按照以下格式将日期存储在数据库中:

2016-10-11T11:09:00

在桌面应用程序中,它正确显示为 2016/10/11 11:09:00

然而,在移动应用程序中(我们使用phonegap,基于HTML + JS),当我通过PHP调用从数据库中获取此日期字符串时,我将其转换为使用'新日期(" 2016-10-11T11:09:00")',它显示时区转换,所以例如如果我在GMT + 2中,它将是 2016/10/11 13:09:00

我该如何防止这种情况?

每当我在视图上显示日期时,我都会使用date.toString(" yyyy / MM / dd HH:mm:ss")格式化。

此外,在存储和显示跨多个平台的日期时间方面,我可以接受任何建议。我发现这个 yyyy-mm-ddThh:mm:ss 格式非常好并且使用得很好,直到我碰到这个javascript问题。

(据我所知,我不是javascript或HTML专家)

LE:我不想将日期转换为UTC字符串,我希望实际的Date对象与数据库值保持一致,因为我将它连接到HTML日期时间选择器AngularJS。

4 个答案:

答案 0 :(得分:1)

你真正想要达到的目标是什么?如果您希望始终显示时间戳,因为它们忽略了时区信息,那么当这些时间戳可能显示某些用户的未来时间时,您可能会使用户感到非常困惑。例如:有人修改数据,美国西海岸的另一个用户查看数据并查看指向未来的时间戳。

也许您要使用的是Data.toUTCString(),它显示时间戳,以便您将GMT作为时区。然后很明显,对于用户来说,这个时间戳不在他们的时区。此外,您可能希望将时区信息附加到时间戳字符串,以便正确地进行转换。如果您的时间戳是UTC(或Zulu时间),您可以添加" Z"到字符串的末尾。 RFC 2822第3.3节定义了日期和时间应该表示本地时间,但至少对于node.js,如果你留出时区信息,它似乎表示UTC时间。因此,添加时区信息将使您的生活更轻松(如果您要在数据库中存储的字符串中添加时区信息,那就更好了。)

举个例子:

var timestamp = "2016-10-11T11:09:00";
var d = new Date(timestamp);
// or
d = new Date(timestamp + "Z");

// Then showing the date
d.toString();
// or without the converting it to user's local timezone
d.toUTCString();

我希望这会有所帮助。

答案 1 :(得分:0)

您可以使用此小功能获取结果

function getCorrectString(dateTimeString){
     var a  = new Date(dateTimeString);
     return a.getFullYear()+'/'
     +('00'+(a.getMonth()+1)).substring(('00'+(a.getMonth()+1)).length-2) +'/'
     +('00'+a.getDate()).substring(('00'+a.getDate()).length-2)+' '
     +('00'+a.getHours()).substring(('00'+a.getHours()).length-2)+':'
     +('00'+a.getMinutes()).substring(('00'+a.getMinutes()).length-2)+':'
     +('00'+a.getSeconds()).substring(('00'+a.getSeconds()).length-2);
}

getCorrectString('2016-10-11T11:09:00')

//result :
//"2016/10/11 11:09:00"

答案 2 :(得分:0)

陷入同样的​​问题。以yyyy-mm-ddThh:mm:ss格式存储,但需要在客户端将其转换为日期以进行倒数计时器计算(需要创建第二个日期 - >减去差异 - >创建倒数计时器)。逻辑在浏览器中是正确的,在Ionic2 / cordova app webviews中不正确和不一致。有趣的是:我和我的妻子都拥有相同的手机品牌/型号,日期会显示出与我不同的日期(!)。

Haven已经过广泛测试,但这里有一个小功能,可以接收字符串并返回日期...在我的手机和浏览器上一致地工作:

const someDateStrFromServer: string = "2017-08-26T15:30:25";
const date: Date = DateUtils.overrideTimezone(someDateStringFromServer);

// these methods are in Class DateUtils {}
// input: string, output: Date
public static overrideTimezone(dateStr: string): Date {
    const date: Date = new Date(dateStr);
    const hours: number = date.getHours();
    const tIndex: number = dateStr.indexOf("T");
    const dateStrHours: number = +(dateStr.slice(tIndex + 1, tIndex + 3));
    const tzOffset =  DateUtils.getTimezoneOffset("h");

    // compares hours sliced from string and hours from date. if different,
    // then I know timezone was calculated into the Date

    if (dateStrHours === hours) return date;  // everything's OK

    // webview factored in timezone, so add it back to correct
    else return new Date(date.setHours(date.getHours() + tzOffset));

}

// can scale it to include "ms", "s", "m", etc
public static getTimezoneOffset(unit = "h") {
    switch(unit) {
        case "h": 
            return (new Date()).getTimezoneOffset() / 60;
        default:
            return (new Date()).getTimezoneOffset() / 60;
    }
}

答案 3 :(得分:0)

您可以使用Moment.js并定义自己的自定义日期格式化程序:

var formatter= 'YYYY-MM-DD[T]HH:mm:ss';
var date = new Date('July 17, 2018 03:24:00');
var time = moment(date).format(formatter);
console.log('Time: ',time);

小提琴:https://jsfiddle.net/mkotsollaris/3krdttm3/

相关问题