Convert Time to Other Timezone

Convert date to another timezone in JavaScript

Here is the one-liner:

function convertTZ(date, tzString) {
return new Date((typeof date === "string" ? new Date(date) : date).toLocaleString("en-US", {timeZone: tzString}));
}

// usage: Asia/Jakarta is GMT+7
convertTZ("2012/04/20 10:10:30 +0000", "Asia/Jakarta") // Tue Apr 20 2012 17:10:30 GMT+0700 (Western Indonesia Time)

// Resulting value is regular Date() object
const convertedDate = convertTZ("2012/04/20 10:10:30 +0000", "Asia/Jakarta")
convertedDate.getHours(); // 17

// Bonus: You can also put Date object to first arg
const date = new Date()
convertTZ(date, "Asia/Jakarta") // current date-time in jakarta.

How to convert javascript date to a particular timezone

I understand that you wanted to get a same time irrespective of time zone when ever you pass the milliseconds.

instead of the following two lines

    var d = new Date(dt)
var os = d.getTimezoneOffset();

you include the below lines

    var os = new Date()
os = os.getTimezoneOffset();
var d = new Date((dt + (os * 60 * 1000)));

This adjusts your milliseconds to UTC time based on your local timezone and creates the date object from the adjusted UTC time, so that you will always get same time, irrespective of your local time zone.

Convert Date to another timezone in JavaScript and Print with Correct Timezone

A few things:

  • The approach in the answer you linked to for the convertTZ is flawed. One should not parse the output of toLocalString with the Date constructor. Please read the comments following that answer.

  • The Date object can not be converted to another time zone, because it is not actually in any time zone. The only thing encapsulated by the Date object is its Unix timestamp, which can be seen with .valueOf(), .getTime(), or any mechanism that coerces it to a Number. Such values are always UTC-based.

  • When you see local time or a time zone coming from the Date object, the system's local time zone is applied to the internal UTC-based Unix Timestamp to create the output. The local time zone cannot be changed programmatically from JavaScript (or TypeScript), nor can it be altered on a per-object basis.

  • Instead of trying to convert a Date object in one time zone to a Date object in another time zone, recognize that all Date objects are inherently UTC. Thus, you can create a string that is in a different time zone, (for example, using toLocaleString with the timeZone option) but you cannot create a new Date object from that string. If you want an object that can actually be set to a different time zone, consider Luxon. If you need a solution without a library, you will one day be able to use a ZonedDateTime from the TC39 Temporal Proposal, which will eventually be part of ECMAScript.

  • Beware of calling console.log on a Date object. Neither the ECMAScript spec nor the WhatWG Console spec defines the behavior of such a call. It is undefined behavior. In some environments, the string output logged is the same as calling .toString() on the Date object - which will give the local time equivalent of the Date object's timestamp, along with a display name for the local time zone. Other environments will show the same output of calling .toISOString(), which will give an ISO-8601 representation of the timestamp in UTC. Instead of logging Date objects, call one of those two functions yourself and log the output.

How do you convert dates/times from one time zone to another in R?

First, convert the London time to a POSIXct object:

pb.txt <- "2009-06-03 19:30"
pb.date <- as.POSIXct(pb.txt, tz="Europe/London")

Then use format to print the date in another time zone:

> format(pb.date, tz="America/Los_Angeles",usetz=TRUE)
[1] "2009-06-03 11:30:00 PDT"

There are some tricks to finding the right time zone identifier to use. More details in this post at the Revolutions blog: Converting time zones in R: tips, tricks and pitfalls

Convert Date/Time for given Timezone - java

For me, the simplest way to do that is:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

//Here you say to java the initial timezone. This is the secret
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
//Will print in UTC
System.out.println(sdf.format(calendar.getTime()));

//Here you set to your timezone
sdf.setTimeZone(TimeZone.getDefault());
//Will print on your default Timezone
System.out.println(sdf.format(calendar.getTime()));

Convert time and date from one time zone to another in PHP

<?php
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";

$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>

The above examples will output:

2000-01-01 00:00:00+12:00
2000-01-01 01:45:00+13:45

found on DateTime Manual on php.net

EDIT:
Like Pekka said: The DateTime class exists from 5.2 on and there you first have to find out which of the methods are realy implemented and which one only exist from 5.3 on.

How to convert Moment to another moment depending on timezone

You can use moment local() to convert moment object to user local time, while you can use tz() to convert time between zones.

Use moment.utc to parse date as UTC. Use format() to show values of moment objects.

If you just need to support UTC and user local time, you don't need moment-timezone.

Here a working sample:

var dt = document.getElementById("utc-time").innerHTML;  var m = moment.utc(dt, "DD-MM-YYYY h:mm:ss A"); // parse input as UTCconsole.log(m.clone().local().format("DD-MM-YYYY h:mm:ss A")); // local timevar tz = 'America/New_York'; // example value, you can use moment.tz.guess()console.log(m.clone().tz(tz).format("DD-MM-YYYY h:mm:ss A")); // 30-03-2017 2:34:22 AMtz = 'Australia/Perth';console.log(m.clone().tz(tz).format("DD-MM-YYYY h:mm:ss A")); // 30-03-2017 2:34:22 PM
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.7/moment-timezone-with-data-2010-2020.min.js"></script>
<div id="utc-time">30/03/2017 6:34:22 AM</div>

Convert date to different timezone

When you print the date using System.out.println(date); or System.out.println(instance2.getTime());, the Date returned by instance2.getTime() is TimeZone independent and always prints the date in local timezone.

Instead you may want to use DateFormat/SimpleDateFormat:

  DateFormat formatter= new SimpleDateFormat("MM/dd/yyyy HH:mm:ss Z");
formatter.setTimeZone(TimeZone.getTimeZone("Europe/London"));
System.out.println(formatter.format(date));

formatter.setTimeZone(TimeZone.getTimeZone("Europe/Athens"));
System.out.println(formatter.format(instance2.getTime()))

converting a time to a different timezone with php

Use the date_default_timezone_set() function of PHP.

If you want to change it to France you would use the

date_default_timezone_set('Europe/Paris');

a list of Supported Timezones can be found here:
http://www.php.net/manual/en/timezones.php

The functionality of date_default_timezone_set() can be found here:
http://php.net/manual/en/function.date-default-timezone-set.php



Related Topics



Leave a reply



Submit