如何将JavaScript日期转换为UTC?

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

2009-1-1 to 2009-1-3

您需要将此日期发送到服务器进行某些处理,但是服务器希望所有日期和时间都采用UTC。

现在,假设用户位于阿拉斯加,夏威夷或斐济。由于它们所处的时区与UTC完全不同,因此需要将日期范围转换为以下形式:

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

使用JavaScript Date对象,您如何将第一个“本地化”日期范围转换为服务器可以理解的范围?

Green理查德2020/03/12 14:19:11

const event = new Date();

console.log(event.toUTCString());

梅猪猪2020/03/12 14:19:11

Even simpler

myvar.setTime(myvar.getTime() + myvar.getTimezoneOffset() * 60000);
西门小小2020/03/12 14:19:11

So this is the way I had to do it because i still wanted a JavaScript date object to manipulate as a date and unfortunantly alot of these answers require you to go to a string.

//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 
猿十三2020/03/12 14:19:11

I know this question is old, but was looking at this same issue, and one option would be to send date.valueOf() to the server instead. the valueOf() function of the javascript Date sends the number of milliseconds since midnight January 1, 1970 UTC.

valueOf()

樱阿飞2020/03/12 14:19:11

Looking at your question its clear that you just want to send the date range to your backend for further post processing.

I am assuming you are conforming to the standard data guidelines which expect the data to be in a particular format. For example, I use ODATA which is a RESTfull API which expects date time objects to be in the format:-

YYYY-MM-DDT00:00:00.

That can be easily achieved via the snippet posted below(Please change the format as per your requirement).

var mydate;//assuming this is my date object which I want to expose var UTCDateStr = mydate.getUTCFullYear() + "-" + mydate.getUTCMonth() + "-" + mydate.getUTCDate() + "T00:00:00";

If on the other hand, you are in my situation wherein you have received a date from your backend, and the browser converts that to your local date. You on the other hand are interested in the UTC date then you can perform the following:-

var mydate;//assuming this is my date object which I want to expose var UTCDate = new Date(mydate);/*create a copy of your date object. Only needed if you for some reason need the original local date*/ UTCDate.setTime(UTCDate.getTime() + UTCDate.getTimezoneOffset() * 60 * 1000);

The code snippet above basically adds/subtracts the time added/subtracted by the browser based on the timezone.

For example if I am in EST(GMT-5) and my Service returns a date time object = Wed Aug 17 2016 00:00:00 GMT-0500 my browser automatically subtracts the timezone offset(5hrs) to get my local time. So if I try to fetch the time I get Wed Aug 16 2016 19:00:00 GMT-0500. This causes a lot of problems. There are a lot of libraries out there which will definitely make this easier but I wanted to share the pure JS approach.

For more info please have a look at: http://praveenlobo.com/blog/how-to-convert-javascript-local-date-to-utc-and-utc-to-local-date/ where in I got my inspiration.

Hope this helps!

乐乐2020/03/12 14:19:11

This is what I have done in the past:

var utcDateString = new Date(new Date().toUTCString()).toISOString();
Sam梅小胖2020/03/12 14:19:11

This function works beautifully for me.

function ParseDateForSave(dateValue) {
    // create a new date object
    var newDate = new Date(parseInt(dateValue.substr(6)));

    // return the UTC version of the date
    return newDate.toISOString();
}
宝儿猴子2020/03/12 14:19:10
var myDate = new Date(); // Set this to your date in whichever timezone.
var utcDate = myDate.toUTCString();
理查德小哥2020/03/12 14:19:10

简单而愚蠢

var date = new Date(); 
var now_utc =  Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(),
 date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());

 return new Date(now_utc);
十三泡芙2020/03/12 14:19:10

这是我的方法:

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

生成的utc对象实际上并不是UTC日期,而是将本地日期更改为与UTC时间匹配(请参见注释)。但是,在实践中它确实可以做到。

卡卡西理查德2020/03/12 14:19:10

转换为ISO而不更改日期/时间

var now = new Date(); // Fri Feb 20 2015 19:29:31 GMT+0530 (India Standard Time) 
var isoDate = new Date(now.getTime() - now.getTimezoneOffset() * 60000).toISOString();
//OUTPUT : 2015-02-20T19:29:31.238Z

更改日期/时间转换为ISO(日期/时间将更改)

isoDate = new Date(now).toISOString();
//OUTPUT : 2015-02-20T13:59:31.238Z 

小提琴链接