0

I need to change the local or current TimeZone to Default UTC (GMT+0000) time zone.

I used the predefined javascript function toLocaleString, but some machines has their own datetime format so the service request gets mismatched.

My requirement is just change the TimeZone without changing the current value (NO CONVERSTION)

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

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

Required Format is Fri Jul 07 2017 17:07:32 GMT+0000 (???)

Scenario: I'm selecting DOB in DatePicker (Angular Material), Its returning the datetime offset value. If I pass those value from client to Server, it automatically converts it to Local to 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. Because I'm in INDIA (UTC +0530), so it subtracts the 05 Hours and 30 Minutes. This is the issue I'm facing.

Kindly assist me.

  • The TimeZone is set in the browser isn't it? – Neil Jul 07 '17 at 11:48
  • Yes, I need to pass the Client's Datetime as a UTC's default datetime. So, I used the javascript predefined method `toLocaleString` but in some machine it converts the datetime with some unappropriated format. I debugged the code, it converts based on that local machines setting. So, I can't able to use this method. I need a solution so... –  Jul 07 '17 at 11:52
  • https://stackoverflow.com/questions/308683/how-can-i-get-the-current-date-and-time-in-utc-or-gmt-in-java – Neil Jul 07 '17 at 11:54
  • You can try dtime.getUTCFullYear(), getUTCDate(), getUTCMonth(), getUTCHours(), getUTCMinutes(), getUTCSeconds(), getUTCMilliseconds() functions and then combine like this: return year + '-' + day + '-' + month + 'T' + hour + ':' + minute + ':' + second + '.' + millisecond + 'Z'; or you can try as per my answer below. – brijrajsinh Jul 07 '17 at 11:57
  • It's easy enough to do what you asked, but before I answer, could you please elaborate as to *why* you want to do this? "My requirement is..." isn't very helpful. I have extensive experience in this area and I can imagine virtually no scenario where what you asked for would be the right thing to do. It would be sad if you introduced a critical bug in your system because you believed it was the requirement. – Matt Johnson-Pint Jul 07 '17 at 16:54
  • @MattJohnson - For example I'm selecting DOB in DatePicker (Angular Material), Its returning the datetime offset value, If I pass those value from client to Server, it automatically converts it to UTC. 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`. Because I'm in INDIA (UTC +0530), so it subtracts the 05Hours and 30 Minutes. This is the issue I'm facing. –  Jul 09 '17 at 00:16
  • So then, this is clearly [an xy problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Your approach is not the right way to solve the real problem at hand. Please close this question and ask a new one, specifically about that. Also, search first. It's been asked a lot already. – Matt Johnson-Pint Jul 09 '17 at 02:21

1 Answers1

-1

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). While on Service it converts it as 1990-01-01 00:00:00.

I think it will solve your problem.

B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130