简体   繁体   English

使用Java脚本仅将本地TimeZone更改为Default UTC

[英]Change only the local TimeZone to Default UTC using Javascript

I need to change the local or current TimeZone to Default UTC ( GMT+0000 ) time zone. 我需要将本地或当前的时区更改为默认UTC( GMT+0000 )时区。

I used the predefined javascript function toLocaleString , but some machines has their own datetime format so the service request gets mismatched. 我使用了预定义的javascript函数toLocaleString ,但是某些机器具有自己的日期时间格式,因此服务请求不匹配。

My requirement is just change the TimeZone without changing the current value ( NO CONVERSTION ) 我的要求是仅更改TimeZone而不更改当前值( NO CONVERSTION

let dtime = new Date();
console.log(dtime);

Output: Fri Jul 07 2017 17:07:32 GMT+0530 (India Standard Time) 输出: Fri Jul 07 2017 17:07:32 GMT+0530 (India Standard Time)

Required Format is Fri Jul 07 2017 17:07:32 GMT+0000 (???) 必填格式为Fri Jul 07 2017 17:07:32 GMT+0000 (???)

Scenario : I'm selecting DOB in DatePicker ( Angular Material ), Its returning the datetime offset value. 场景 :我在DatePickerAngular Material )中选择DOB,它返回datetime offset值。 If I pass those value from client to Server , it automatically converts it to Local to UTC Default. 如果我将这些值从客户端传递给Server ,它将自动将其转换为Local到UTC Default。

For Example : I'm selecting DOB as 1990-01-01 in datepicker , it returns the value 1990-01-01 00:00:00 but in HTTP call it goes as 1989-12-31 18:30:00 . 例如 :我在datepicker选择DOB作为1990-01-01 ,它返回值1990-01-01 00:00:00但是在HTTP调用中它变成1989-12-31 18:30:00 Because I'm in INDIA ( UTC +0530 ) , so it subtracts the 05 Hours and 30 Minutes. 因为我在印度( UTC +0530 )中 ,所以它减去了05小时30分钟。 This is the issue I'm facing. 这是我面临的问题。

Kindly assist me. 请帮助我。

You can try the following piece of code 您可以尝试以下代码

date.setMinutes((date.getTimezoneOffset() * -1));

No need of localization, use this code just before doing any service call. 无需本地化,请在进行任何服务调用之前使用此代码。 It will pass you the exact date what you selected in the datepicker. 它将为您传递您在日期选择器中选择的确切日期。

It will work in all timezone (+) and (-), 它将在所有时区(+)和(-)中工作,

Example: 1990-01-01 00:00:00 GMT+0530 (India Standard Time) , the above said code covert it as 1990-01-01 05:30:00 GMT+0530 (India Standard Time) . 例如: 1990-01-01 00:00:00 GMT+0530 (India Standard Time) ,上面的代码将其隐藏为1990-01-01 05:30:00 GMT+0530 (India Standard Time) While on Service it converts it as 1990-01-01 00:00:00 . 在服务期间,它将其转换为1990-01-01 00:00:00

I think it will solve your problem. 我认为它将解决您的问题。

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

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