-2

I have one date time

Date_Dubai=2017-12-29 01:00 AM Asia/Dubai

and i have to convert it to "Asia/Kolkota" in "Date_Dubai" format.

Anoop Babu
  • 178
  • 12

1 Answers1

0
 dt = new Date();
 localTime = dt.getTime(); //current localtime in milisecond.
 localOffset = dt.getTimezoneOffset() * 60000; 
//getTimezoneOffset() returns in minutes so converting it into millisecond(*60000). 

The time-zone offset is the difference, in minutes, between UTC and local time. Note that this means that the offset is positive if the local timezone is behind UTC and negative if it is ahead.(Doc)

Get the current UTC time, by adding the local time zone offset to the local time.

 utc = localTime + localOffset;

After getting UTC time, get the destination city's UTC offset in hours. Here Dubai offset is +4 hours.

    offset = 4; // GST (Gulf Standard Time) ahead +4 hours from utc
    dubaiTime = utc + (3600000*offset); // convert offset into milisecond and add to UTC time.
    newTime = new Date(dubaiTime); 
    console.log(newTime); 

More info about Date Object

Akhilendra
  • 1,137
  • 1
  • 16
  • 31
  • 1
    Please explain why your solution works, if it does, to improve the quality of your answer. – Eric Hauenstein Dec 26 '17 at 14:51
  • 1
    @EricHauenstein Hope it is fine now. – Akhilendra Dec 26 '17 at 18:01
  • -1. This approach is given often, and is *always* wrong. You cannot convince the `Date` object to change its time zone. All you're doing here is picking a different point in time. Consider if you called `getTimezoneOffset()` on the resulting `newTime` variable - it would still be that of the local computer - not Dubai. This leads to errors in time zones that have transitions, such as daylight saving time or a change in standard time. – Matt Johnson-Pint Dec 27 '17 at 20:05