假设您网站的用户输入了一个日期范围。

2009-1-1 to 2009-1-3

您需要将此日期发送到服务器进行某些处理,但服务器要求所有日期和时间均为UTC。

现在假设用户在阿拉斯加。由于它们所处的时区与UTC完全不同,因此需要将日期范围转换为如下所示:

2009-1-1T8:00:00 to 2009-1-4T7:59:59

使用JavaScriptDate对象,如何将第一个“本地化”日期范围转换为服务器能够理解的内容?


当前回答

对于目标是将其作为“日期对象”而不是字符串获取的其他人,并且您只想显示不带TZ(可能是硬编码的)的日期/时间,您可以做的是:

const now = new Date();
const year = now.getUTCFullYear();
const month = now.getUTCMonth();
const day = now.getUTCDate();
const hour = now.getUTCHours();

const tomorrowUTC= new Date();
tomorrowUTC.setDate(day + 1); // +1 because my logic is to get "tomorrow"
tomorrowUTC.setYear(year);
tomorrowUTC.setMonth(month);
tomorrowUTC.Hours(hour);

// then use the tomorrowUTC for to display/format it
// tomorrowUTC is a "Date" and not a string.

然后,您可以执行以下操作:

我们将在${格式(明天UTC,'EEEE do MMMM hh:mmaa')}UTC删除您的帐户

(格式是一个日期fns函数,如果需要,可以使用其他lib);

这有点“黑客”,因为这仍然使用本地时区,但如果你只想显示日期而不是时区,那么这就可以了。

其他回答

Date.prototype.toUTCArray= function(){
    var D= this;
    return [D.getUTCFullYear(), D.getUTCMonth(), D.getUTCDate(), D.getUTCHours(),
    D.getUTCMinutes(), D.getUTCSeconds()];
}

Date.prototype.toISO= function(){
    var tem, A= this.toUTCArray(), i= 0;
    A[1]+= 1;
    while(i++<7){
        tem= A[i];
        if(tem<10) A[i]= '0'+tem;
    }
    return A.splice(0, 3).join('-')+'T'+A.join(':');    
}

这是我的方法:

var now = new Date();
var utc = new Date(now.getTime() + now.getTimezoneOffset() * 60000);

生成的utc对象实际上不是utc日期,而是转换为与utc时间匹配的本地日期(请参见注释)。然而,在实践中,它确实起到了作用。


更新:当调用UTC.toString()、UTC.toLocaleString()等时,这个答案是获取UTC日期的一种快速而肮脏的方法。不过,有更好的解决方案,尤其是现在使用现代浏览器的情况下,我应该改进一下答案。基本上,.toISOString()(IE 9+)是您想要使用的。

所以这是我必须要做的,因为我仍然希望JavaScript日期对象作为日期进行操作,而不幸的是,这些答案中的很多都需要您转到字符串。

//First i had a string called stringDateVar that i needed to convert to Date
var newDate = new Date(stringDateVar)

//output: 2019-01-07T04:00:00.000Z
//I needed it 2019-01-07T00:00:00.000Z because i had other logic that was dependent on that 

var correctDate = new Date(newDate.setUTCHours(0))

//This will output 2019-01-07T00:00:00.000Z on everything which allows scalability 

处理日期时,我的建议是将日期解析为用户输入的各个字段。你可以把它当作一根完整的绳子,但你在玩火。

JavaScript可以以不同的格式处理两个相同的日期。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

永远不要做类似的事情:

new Date('date as text');

一旦您将日期从用户输入解析到其各个字段中,就创建一个日期对象。创建日期对象后,通过添加时区偏移将其转换为UTC。由于DST,我无法强调使用日期对象的偏移量有多重要(这是另一个讨论来说明原因)。

var year = getFullYear('date as text');
var month = getMonth('date as text');
var dayOfMonth = getDate('date as text');

var date = new Date(year, month, dayOfMonth);

var offsetInMs = ((date.getTimezoneOffset() * 60)  // Seconds
                 * 1000);                          //  Milliseconds

var utcDate = new Date(date.getTime + offsetInMs);

现在,您可以将日期以UTC时间传递给服务器。我再次强烈建议不要使用任何日期字符串。要么将其传递给服务器,并将其细分为所需的最低粒度,例如年、月、日、分钟,要么将其作为一个值(如unix时期的毫秒)传递给服务器。

getTimezoneOffset()方法返回时区差异,以分钟,从当前区域设置(主机系统设置)到UTC。

来源:MDN网络文档

这意味着,如果本地时区晚于UTC,则偏移量为正,如果超前,则偏移为负。例如,对于时区UTC+02:00,将返回-120。

let d=新日期();console.log(d);d.setTime(d.getTime()+(d.getTimezoneOffset()*60000));console.log(d);

注意:这会将日期对象时间转换为UTC±00:00,而不会转换其时区,因此日期对象时区仍然相同,但值将为UTC±0:00。