Convert UTC datetime string to local datetime
If you don't want to provide your own tzinfo
objects, check out the python-dateutil library. It provides tzinfo
implementations on top of a zoneinfo (Olson) database such that you can refer to time zone rules by a somewhat canonical name.
from datetime import datetime
from dateutil import tz
# METHOD 1: Hardcode zones:
from_zone = tz.gettz('UTC')
to_zone = tz.gettz('America/New_York')
# METHOD 2: Auto-detect zones:
from_zone = tz.tzutc()
to_zone = tz.tzlocal()
# utc = datetime.utcnow()
utc = datetime.strptime('2011-01-21 02:37:21', '%Y-%m-%d %H:%M:%S')
# Tell the datetime object that it's in UTC time zone since
# datetime objects are 'naive' by default
utc = utc.replace(tzinfo=from_zone)
# Convert time zone
central = utc.astimezone(to_zone)
Edit Expanded example to show strptime
usage
Edit 2 Fixed API usage to show better entry point method
Edit 3 Included auto-detect methods for timezones (Yarin)
Java Convert GMT/UTC to Local time doesn't work as expected
I also recommend using Joda as mentioned before.
Solving your problem using standard Java Date
objects only can be done as follows:
// **** YOUR CODE **** BEGIN ****
long ts = System.currentTimeMillis();
Date localTime = new Date(ts);
String format = "yyyy/MM/dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
// Convert Local Time to UTC (Works Fine)
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date gmtTime = new Date(sdf.format(localTime));
System.out.println("Local:" + localTime.toString() + "," + localTime.getTime() + " --> UTC time:"
+ gmtTime.toString() + "," + gmtTime.getTime());
// **** YOUR CODE **** END ****
// Convert UTC to Local Time
Date fromGmt = new Date(gmtTime.getTime() + TimeZone.getDefault().getOffset(localTime.getTime()));
System.out.println("UTC time:" + gmtTime.toString() + "," + gmtTime.getTime() + " --> Local:"
+ fromGmt.toString() + "-" + fromGmt.getTime());
Output:
Local:Tue Oct 15 12:19:40 CEST 2013,1381832380522 --> UTC time:Tue Oct 15 10:19:40 CEST 2013,1381825180000
UTC time:Tue Oct 15 10:19:40 CEST 2013,1381825180000 --> Local:Tue Oct 15 12:19:40 CEST 2013-1381832380000
Convert UTC date time to local date time
Append 'UTC' to the string before converting it to a date in javascript:
var date = new Date('6/29/2011 4:52:48 PM UTC');
date.toString() // "Wed Jun 29 2011 09:52:48 GMT-0700 (PDT)"
How to convert from GMT/UTC to local time and back using momentjs?
Since your input represent UTC time, you have to use moment.utc
(as suggested by others in the comments):
By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC, you can use
moment.utc()
instead ofmoment()
.
Moreover, since your input string is not in a format recognized by moment(String)
(ISO 8601 or RFC 2822), you have to pass format parameter as second argument of moment.utc
, see String + Format parsing section of the docs.
Here a code sample:
var input = "December 25, 2018 01:02:20";
var fmt = 'MMMM DD, YYYY HH:mm:ss';
var m = moment.utc(input, fmt);
console.log(m.local().format(fmt));
console.log(m.utc().format(fmt));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>
Converting Between Local Times and GMT/UTC in C/C++
You're supposed to use combinations of gmtime
/localtime
and timegm
/mktime
. That should give you the orthogonal tools to do conversions between struct tm
and time_t
.
For UTC/GMT:
time_t t;
struct tm tm;
struct tm * tmp;
...
t = timegm(&tm);
...
tmp = gmtime(t);
For localtime:
t = mktime(&tm);
...
tmp = localtime(t);
All tzset()
does is set the internal timezone variable from the TZ
environment variable. I don't think this is supposed to be called more than once.
If you're trying to convert between timezones, you should modify the struct tm
's tm_gmtoff
.
Convert UTC dates to local time in PHP
date()
and localtime()
both use the local timezone for the server unless overridden; you can override the timezone used with date_default_timezone_set()
.
http://www.php.net/manual/en/function.date-default-timezone-set.php
http://us3.php.net/manual/en/function.date.php
http://php.net/manual/en/function.localtime.php
Related Topics
Parsing CSV Files in C#, With Header
Split List into Sublists With Linq
Identify If a String Is a Number
C# Difference Between == and Equals()
How to Make a Textbox That Only Accepts Numbers
An Async/Await Example That Causes a Deadlock
How to Do Impersonation in .Net
Asynchronously Wait For Task≪T≫ to Complete With Timeout
How to Save Application Settings in a Windows Forms Application
Pass an Instantiated System.Type as a Type Parameter For a Generic Class
Typenamehandling Caution in Newtonsoft Json
Difference Between New and Override
When Do You Use the "This" Keyword
Using New Unity Videoplayer and Videoclip API to Play Video
How to Add a Timeout to Console.Readline()
A Field Initializer Cannot Reference the Nonstatic Field, Method, or Property