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 ?
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 .
The . getTimezoneOffset() method should work. This will get the time between your time zone and GMT. You can then calculate to whatever you want.
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.
UTC time in ISO-8601 is 09:05:16Z. Note that the Z letter without a space.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With