Facebook returns 'created_time' in this format:
2012-07-23T08:52:04+0000
I want to convert this timestamp to a normal Python DateTime object.
Have you tried dateutil
It's extremely easy to use
import dateutil.parser as dateparser
dateparser.parse('2012-07-23T08:52:04+0000')
dateutil is very helpful to deal with timezone info, and it can handle lots of time formats.
s = "2005-12-06T12:13:14"
from datetime import datetime
from time import strptime
print datetime(*strptime(s, "%Y-%m-%dT%H:%M:%S")[0:6])
Related
I am using the telethon library to making telegram bots.
when I using event.date it was printing the time on +00.00 time zone.
How can I print time on specific timezone
See python - Convert UTC datetime string to local datetime. Stealing the top answer which uses python-dateutil:
from dateutil import tz
local_datetime = event.date.astimezone(tz.tzlocal())
from datetime import datetime
print(datetime.now().isoformat(timespec='minutes'))
have a look in datetime library
How do you get time to be formatted like this in python?
2020-12-18T21:43:42Z
What I have tried so far:
from datetime import date
date.fromisoformat('2020-12-18T21:43:42Z')
The format you shared isn't the iso format that datetime uses, but you could explicitly format the datetime yourself using strftime:
from datetime import datetime
result = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ')
I'm trying to convert my date string to ISO format but it's not working. Any suggestions on efficiently doing this with datetime, time, or pandas?
date = "2/3/13"
import datetime
t = datetime.date(*map(int,date.split("/")))
format = "'%Y-%m-%d'"
t.strftime(format)
# "'0002-03-13'"
With pandas -
pd.to_datetime("2/3/13")
Timestamp('2013-02-03 00:00:00')
With pandas, you can (most of the time) infer the format without having to manually specify it. However, it would seem pointless to import such a heavy module to use it only for a simple datetime conversion.
For that reason, here's an equally simple python way using strptime -
from datetime import datetime as dt
dt.strptime("2/3/13", '%d/%m/%y')
datetime.datetime(2013, 3, 2, 0, 0)
Or, even simpler (you don't have to pass a format string), using dateutil -
from dateutil import parser
parser.parse('2/3/13/').date()
datetime.date(2013, 2, 3)
pandas actually uses the dateutil library under the hood (– unutbu). If your date strings are consistent in structure, I'd recommend using one of these.
I love me some libraries when it comes to working with date/times. dateparser, parsedatetime, and arrow are all really good choices
import dateparser as dp
print(dp.parse("2/13/13"))
2013-02-13 00:00:00
print(dp.parse("2/13/13").date())
2013-02-13
print(dp.parse("Yesterday at 2am PST"))
2017-11-27 13:56:59.290385-08:00
Cheers
I'm trying to convert a UNIX time stamp to UTC+9. I've been searching for hours and it's all very confusing what with the different libraries etc
Here's what I've got so far
from datetime import datetime
from pytz import timezone
import datetime
time = 1481079600
utc_time = datetime.datetime.fromtimestamp(time)#.strftime('%Y-%m-%d %H:%M:%S')
print utc_time.strftime(fmt)
tz = timezone('Japan')
print tz.localize(utc_time).strftime(fmt)
This just prints the same time, what am I doing wrong
I am going to shamelessly plug this new datetime library I am obsessed with, Pendulum.
pip install pendulum
import pendulum
t = 1481079600
pendulum.from_timestamp(t).to_datetime_string()
>>> '2016-12-07 03:00:00'
And now to change it to your timezone super quick and easy!
pendulum.from_timestamp(t, 'Asia/Tokyo').to_datetime_string()
>>> '2016-12-07 12:00:00'
Your utc_time datetime is naive - it has no timezone associated with it. localize assigns a timezone to it, it doesn't convert between timezones. The simplest way to do that is probably to construct a timezone-aware datetime:
import pytz
utc_time = datetime.datetime.fromtimestamp(time, pytz.utc)
Then convert to the timezone you want when you're ready to display it:
print utc_time.astimezone(tz).strftime(fmt)
When I convert unix time 1463288494 to isoformat i get 2016-05-14T22:01:34. How can I get the output including the -07:00. In this format 2016-05-14T22:01:34-07:00
from datetime import datetime
t = int("1463288494")
print(datetime.fromtimestamp(t).isoformat())
You can pass a tzinfo instance representing your timezone offset to fromtimestamp(). The problem then is how to get the tzinfo object. The easiest way is to use the pytz module which provides a tzinfo compatible object:
import pytz
from datetime import datetime
tz = pytz.timezone('America/Los_Angeles')
print(datetime.fromtimestamp(1463288494, tz).isoformat())
#2016-05-14T22:01:34-07:00