Display the time in a different time zone
A simpler method: from datetime import datetime from pytz import timezone south_africa = timezone(‘Africa/Johannesburg’) sa_time = datetime.now(south_africa) print sa_time.strftime(‘%Y-%m-%d_%H-%M-%S’)
A simpler method: from datetime import datetime from pytz import timezone south_africa = timezone(‘Africa/Johannesburg’) sa_time = datetime.now(south_africa) print sa_time.strftime(‘%Y-%m-%d_%H-%M-%S’)
To answer my own question, this functionality has been added to pandas in the meantime. Starting from pandas 0.15.0, you can use tz_localize(None) to remove the timezone resulting in local time. See the whatsnew entry: http://pandas.pydata.org/pandas-docs/stable/whatsnew.html#timezone-handling-improvements So with my example from above: In [4]: t = pd.date_range(start=”2013-05-18 12:00:00″, periods=2, freq=’H’, tz= “Europe/Brussels”) In [5]: t … Read more
Seems the most foolproof way to start with a UTC date is to create a new Date object and use the setUTC… methods to set it to the date/time you want. Then the various toLocale…String methods will provide localized output. Example: // This would come from the server. // Also, this whole block could probably … Read more
You probably need to put the timezone in a configuration line in your php.ini file. You should have a block like this in your php.ini file: [Date] ; Defines the default timezone used by the date functions ; http://php.net/date.timezone date.timezone = America/New_York If not, add it (replacing the timezone by yours). After configuring, make sure … Read more
To summarize Matt Johnson’s answer in terms of code: <script type=”text/javascript” src=”https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js”> </script> <script type=”text/javascript” src=”https://cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.4/jstz.min.js”> </script> <script type=”text/javascript”> $(document).ready(function(){ var tz = jstz.determine(); // Determines the time zone of the browser client var timezone = tz.name(); //For e.g.:”Asia/Kolkata” for the Indian Time. $.post(“url-to-function-that-handles-time-zone”, {tz: timezone}, function(data) { //Preocess the timezone in the controller function … Read more
This will get you the timezone as a PHP variable. I wrote a function using jQuery and PHP. This is tested, and does work! On the PHP page where you are want to have the timezone as a variable, have this snippet of code somewhere near the top of the page: <?php session_start(); $timezone = … Read more
You can use the parse function from dateutil: >>> from dateutil.parser import parse >>> d = parse(‘2009/05/13 19:19:30 -0400’) >>> d datetime.datetime(2009, 5, 13, 19, 19, 30, tzinfo=tzoffset(None, -14400)) This way you obtain a datetime object you can then use. As answered, dateutil2.0 is written for Python 3.0 and does not work with Python 2.x. … Read more
Jon’s answer talks about TimeZone, but I’d suggest using TimeZoneInfo instead. Personally I like keeping things in UTC where possible (at least for the past; storing UTC for the future has potential issues), so I’d suggest a structure like this: public struct DateTimeWithZone { private readonly DateTime utcDateTime; private readonly TimeZoneInfo timeZone; public DateTimeWithZone(DateTime dateTime, … Read more
tl;dr ZonedDateTime.now( ZoneId.of( “Pacific/Auckland” )) // Current moment in a particular time zone. .withZoneSameInstant( ZoneId.of( “Asia/Kolkata” )) // Same moment adjusted into another time zone. Details The java.util.Date class has no time zone assigned†, yet it’s toString implementation confusingly applies the JVM’s current default time zone. Avoid java.util.Date & .Calendar This is one of many … Read more
I recommend using python-dateutil. Its parser has been able to parse every date format I’ve thrown at it so far. >>> from dateutil import parser >>> parser.parse(“Tue Jun 22 07:46:22 EST 2010”) datetime.datetime(2010, 6, 22, 7, 46, 22, tzinfo=tzlocal()) >>> parser.parse(“Fri, 11 Nov 2011 03:18:09 -0400”) datetime.datetime(2011, 11, 11, 3, 18, 9, tzinfo=tzoffset(None, -14400)) >>> … Read more