Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get time of specific timezone [duplicate]

I am using a JavaScript Date class & trying to get the current date using getDate() method. But obviously it is loading system date & time. I am running the code from India but I want to get the date & time of UK using the same method. How can I do that ?

like image 427
aslamdoctor Avatar asked Nov 21 '11 05:11

aslamdoctor


People also ask

How to get time in a specific timezone js?

Use the toLocaleString() Method to Initialize JavaScript Date to a Particular Time Zone. The toLocaleString() method is the most commonly used for changing time zone as it is easier to use and can be called directly on a Date. It works in the same sway as the Intl. DateTimeFormat .

How do you find time according to time zones?

The . getTimezoneOffset() method should work. This will get the time between your time zone and GMT. You can then calculate to whatever you want.

How do I convert datetime to another time zone?

ToLocalTime method. It takes a single parameter, which is the date and time value to convert. You can also convert the time in any designated time zone to local time by using the static ( Shared in Visual Basic) TimeZoneInfo. ConvertTime method.

What is UTC time now in 24 hour format?

UTC time in ISO-8601 is 09:05:16Z. Note that the Z letter without a space.


1 Answers

If you know the UTC offset then you can pass it and get the time using the following function:

function calcTime(city, offset) {     // create Date object for current location     var d = new Date();      // convert to msec     // subtract local time zone offset     // get UTC time in msec     var utc = d.getTime() + (d.getTimezoneOffset() * 60000);      // create new Date object for different city     // using supplied offset     var nd = new Date(utc + (3600000*offset));      // return time as a string     return "The local time for city"+ city +" is "+ nd.toLocaleString(); }  alert(calcTime('Bombay', '+5.5')); 

Taken from: Convert Local Time to Another

like image 181
Sudhir Bastakoti Avatar answered Sep 29 '22 22:09

Sudhir Bastakoti