简体   繁体   English

从JavaScript Date对象提取日期字符串,不应用时区

[英]Extracting date strings from the JavaScript Date object without timezones being applied

I want to do a straight conversion of a date string and cannot get the results I expect. 我想对日期字符串进行直接转换,但无法获得预期的结果。 I expect the below code to produce a date equal to the date portion of the original string, but it seems to apply a time during the instance creation. 我希望下面的代码产生的日期等于原始字符串的日期部分,但似乎在实例创建过程中要花费一定的时间。

var usageTime = new Date('2012-01-19T22:59:50-0800');
console.log(usageTime);  // Fri, 20 Jan 2012 06:59:50 GMT

dayOfUsage = usageTime.getFullYear()+'-'+(usageTime.getMonth()+1)+'-'+usageTime.getDate();
console.log(dayOfUsage);  // 2012-1-20

I expect a date of 2012-1-19. 我希望日期为2012-1-19。 What is the right way to solve this? 解决这个问题的正确方法是什么?

您应该使用getUTC*()函数( getUTCDate()getUTCHours()等)来获得没有时区偏移的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM