Skip to content

Commit 4aa22fd

Browse files
author
Amey Jadiye
committed
Python timezone change
1 parent bd435e2 commit 4aa22fd

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
author: ameyjadiye
3+
layout: post
4+
title: "Timezone conversion in python -Easy way"
5+
date: 2014-08-08 19:17
6+
comments: true
7+
tags:
8+
- python
9+
---
10+
11+
This lib i found most usefulll [python-dateutil](http://niemeyer.net/python-dateutil).
12+
```bash
13+
sudo pip install python-dateutil
14+
```
15+
16+
{% highlight python %}
17+
from datetime import datetime
18+
from dateutil import tz
19+
date_str = "2014-08-08 19:18:01"
20+
fmt="%Y-%m-%d %H:%M:%S"
21+
from_zone = tz.gettz('UTC')
22+
to_zone = tz.gettz('America/New_York')
23+
24+
utc = datetime.strptime(date_str, fmt)
25+
utc = utc.replace(tzinfo=from_zone)
26+
print utc.strftime(fmt)
27+
# Convert time zone
28+
central = utc.astimezone(to_zone)
29+
print central.strftime(fmt)
30+
{% endhighlight %}
31+
32+
Happy coding ..... :)

0 commit comments

Comments
 (0)