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.
Java Date Time conversion to given timezone
with the help of @ole v.v's explanation i have separated the datetime value for two
1. time
2. timezone
then i used this coding to extract the datetime which is related to the given timezone
//convert datetime to give timezone
private static String DateTimeConverter (String timeVal, String timeZone)
{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat offsetDateFormat2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
offsetDateFormat2.setTimeZone(TimeZone.getTimeZone(timeZone));
String result =null;
try {
result = offsetDateFormat2.format(format.parse(timeVal));
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
BigQuery converting to a different timezone
2016 update: Look answers below, BigQuery now provides timestamp and timezone methods.
You are right - BigQuery doesn't provide any timestamp conversion methods.
In this case, I suggest that you run your GROUP BY based on dimensions of the GMT/UTC timestamp field, and then convert and display the result in the local timezone in your code.
javascript timezone conversion using momentjs
You can use moment-timezone
like this:
const moment = require('moment-timezone')
const date = '2022-01-10T00:00:00.000Z';
const timezone = 'America/New_York';
const convertedDate = moment.utc(date) // create Moment object from date in UTC
.tz(timezone) // convert to provided time zone
.format('YYYY-MM-DD hh:mm:ss A') // display in format
console.log(convertedDate)
// 2022-01-09 07:00:00 PM
Python (datetime) timezone conversion off by 4 minutes
From pytz documentation:
This library differs from the documented Python API for tzinfo implementations; if you want to create local wallclock times you need to use the
localize()
method documented in this document. In addition, if you perform date arithmetic on local times that cross DST boundaries, the result may be in an incorrect timezone (ie. subtract 1 minute from 2002-10-27 1:00 EST and you get 2002-10-27 0:59 EST instead of the correct 2002-10-27 1:59 EDT).
So, you are using pytz incorrectly.
Following is both correct, and erroneous code Following code shows results of your use of pytz (datetime.replace(tzinfo=pytz.timezone)
), and the recommended way of using pytz with datetime (pytz.timezone.localize(datetime)
).
from datetime import datetime, date, time, timezone
from dateutil import tz
import pytz
d = date(2019, 1, 27)
t = time(19, 32, 00)
t1 = datetime.combine(d, t)
t1_epoch = t1.timestamp()
print("t1_epoch " + str(t1_epoch))
print("t1 " + str(t1))
# your approach/code
nytz = pytz.timezone('America/New_York')
t3 = t1.replace(tzinfo=nytz)
t3_epoch = t3.timestamp()
print("t3_epoch " + str(t3_epoch))
print("t3 " + str(t3))
# recommended approach/code using localize
nytz = pytz.timezone('America/New_York')
t6 = nytz.localize(t1)
t6_epoch = t6.timestamp()
print("t6_epoch " + str(t6_epoch))
print("t6 " + str(t6))
Output of above code:
t1_epoch 1548617520.0
t1 2019-01-27 19:32:00
t3_epoch 1548635280.0
t3 2019-01-27 19:32:00-04:56
t6_epoch 1548635520.0
t6 2019-01-27 19:32:00-05:00
t3
is what you are doing, and it is giving incorrect offset (-4:56). Note that POSIX time is also incorrect in this case. POSIX time, by definition, does not change with timezone.
t6
has been created using pytz.timezone.localize()
method, and gives correct UTC offset (-5:00).
Update: Updated language of the answer as one user found the answer confusing.
Related Topics
Android, Java: Http Post Request
Android Camera Will Not Work. Startpreview Fails
How to Convert a Part of Java Source File to Kotlin
Add Custom Headers to Webview Resource Requests - Android
How to Add a Recyclerview Inside Another Recyclerview
The Following Classes Could Not Be Instantiated: - Android.Support.V7.Widget.Toolbar
How to Use Intent.Flag_Activity_Clear_Top to Clear the Activity Stack
How to Programmatically Set Drawableleft on Android Button
Android Download Binary File Problems
How to Get Android Wifi Scan Results into a List
Getting Frames from Video Image in Android
Access Viewpager Fragment Method from Activity
Math.Random() Versus Random.Nextint(Int)