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')
Related
I have a string "15:15:00"
I need to convert it to timestamp like 1410748201
Python
A UNIX timestamp always needs a relation between a date and a time to actually form it, since it is the number of seconds that are passed since 1970-01-01 00:00. Therefore I would recommend you to use the datetime module.
Let's assume you have given the following format: "2022-10-31 15:15:00".
With datetime you can convert the string into a datetime object by using the strptime() function.
Afterwards, a datetime object gives you the ability to convert your datetime into a UNIX timestamp with the timestamp() method of your datetime object.
from datetime import datetime
datetime_str = "2022-10-31 15:15:00"
datetime_obj = datetime.strptime(datetime_str, "%Y-%m-%d %H:%M:%S")
print(datetime_obj.timestamp())
This question already has answers here:
Convert datetime object to a String of date only in Python
(15 answers)
Closed 4 months ago.
I have a JSON that returns a date in the following date format:
datetime(2015, 12, 1)
So the key value from JSON is
'CreateDate': datetime(2015, 1, 1)
In order to be able to subtract two dates, I need to convert above to date format:
YYYY/MM/DD
so in above case that would become: 2015/12/01
Is there a smart way to do that? Is it at all possible? Or do I really have to parse it as a block of text? I tried using datetime.strptime, but I can't get it to work.
datetime(2015,12,1).strftime("%Y/%m/%d")
will do the trick for you. A complete python program would look like this.
# import the datetime and date classes from the datetime module
from datetime import datetime, date
# create your datetime(..) instance from your JSON somehow
# The included Python JSON tools do not usually know about date-times,
# so this may require a special step
# Assume you have a datetime now
dt = datetime(2015,12,1)
# Print it out in your format
print( dt.strftime("%Y/%m/%d") )
Two important details:
You are using just a date in a Python datetime. Nothing wrong with that but just note that the Python datetime module also has a date class
You can enable your JSON encoder/decoder to recognise dates and datetimes automatically but it requires extra work.
Now, to subtract datetimes from each other they should remain as instances of the datetime class. You can not subtract datetimes from each other once they have been formatted as a string.
Once you subtract a Python datetime from an other datetime the result will be an instance of the timedelta class.
from datetime import datetime, timedelta
# time_diff here is a timedelta type
time_diff = datetime(2015,12,1) - datetime(2014,11,1)
Now you can look up the Python timedelta type and extract the days, hours, minutes etc. that you need. Be aware that timedeltas can be a negative if you subtract a later datetime from an earlier one.
The datetime module has a function for doing this which is pretty easy as shown below
from datetime import datetime
print(datetime(2015,12,1).strftime("%Y/%m/%d"))
Also read more about the module here https://docs.python.org/3/library/datetime.html
I have a python program that ingests and processes data, yet one field is not converting properly. One piece of data comes in as a string of java.time.OffsetDateTime.now(ZoneOffset.UTC) and we use strptime to create a datetime object. Here is the simplified code:
$ python
from datetime import datetime
# source string format: (java.time.OffsetDateTime.now(ZoneOffset.UTC))
date_string = "2021-06-28T19:47:27.510670082Z"
# this works yet is not in the source format
date_string = "2021-06-28T19:47:27.510670"
datetime_object = datetime.strptime(date_string, '%Y-%m-%dT%H:%M:%S.%f')
print (datetime_object)
Results in this error
ValueError: unconverted data remains: 082Z
What is the proper format code to create the datetime object?
Just use dateutil. You have time in ISO 8601 format with setting of timezone
from dateutil import parser
# source string format: (java.time.OffsetDateTime.now(ZoneOffset.UTC))
date_string = "2021-06-28T19:47:27.510670082Z"
datetime_object = parser.parse(date_string)
print(datetime_object) # 2021-06-28 19:47:27.510670+00:00
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
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])