This question already has answers here:
Generate RFC 3339 timestamp in Python [duplicate]
(7 answers)
ISO time (ISO 8601) in Python
(15 answers)
Closed last month.
I have a variable as -
present_date = datetime.datetime(2022, 9, 26, 13, 11, 35, tzinfo=datetime.timezone.utc)
Expected output -
2022-09-26T13:11:35Z
Any help would be appreciated.
You can use isoformat method to convert the datetime object to RFC 3339 format. i.e. your expected output format.
You can do the operation as follows:
present_date.isoformat('T')
Above code will give you output: 2022-09-26T13:11:35+00:00. This output is of type str.
The catch here is that as you mentioned you need Z in your expected output, so as per RFC 3339 format, Z is just a constant written for your timezone. i.e. the part after + sign in output. So you can just replace +00:00 with Z by using string operation.
The Final expression if you want Z in your output would be:
present_date.isoformat('T').replace("+00:00", "Z")
Above code will produce output: 2022-09-26T13:11:35Z
Related
This question already has answers here:
Converting unix timestamp string to readable date
(19 answers)
Closed 1 year ago.
I have a very silly question... how I can convert this timestring to a normal datetime?
20170509.54166667
Greetings.
You can do:
import datetime
datetime.datetime.fromtimestamp(20170509.54166667)
# which returns this:
datetime.datetime(1970, 8, 22, 11, 55, 9, 541667)
This question already has answers here:
How to truncate the time on a datetime object?
(18 answers)
How to display locale sensitive time format without seconds in python
(4 answers)
Closed 4 years ago.
I have a python datetime object that I want to display on a website, however the time shows in the format hh:mm:ss and I want to display it in the format hh:mm.
I have tried using the replace method as per the following:
message.timestamp.replace(second='0', microsecond=0)
However this doesn't get red of the seconds it just replaces it with hh:mm:00
Use strftime() function
>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2018, 12, 27, 11, 14, 37, 137010)
>>> now = datetime.now()
>>> now.strftime("%H:%M")
'11:15'
This question already has answers here:
Converting unix timestamp string to readable date
(19 answers)
Closed 7 years ago.
The last.fm API using user.getRecentTracks method's response supplies a date in the following format:
"date": {
"#text": "11 Dec 2015, 01:41",
"uts": "1449798068"
},
What is this "uts" field and how do I convert it into datetime string for a MySql database in python? Would your suggested answer be more efficient than using datetime.datetime.strptime() method to convert the text string given?
uts looks like a timestamp. The abbreviation probably stands for UTC timestamp or Unix Timestamp. I'm not sure which, but, it simple to convert it to a datetime object
>>> from datetime import datetime
>>> dt = datetime.fromtimestamp(1449798068)
>>> print(dt)
datetime.datetime(2015, 12, 10, 20, 41, 8)
It seems the #text key has the time in a local timezone, which is five hours ahead.
This question already has answers here:
How to truncate the time on a datetime object?
(18 answers)
Closed 7 years ago.
My colleague needs me to drop the milliseconds from my python timestamp objects in order to comply with the old POSIX (IEEE Std 1003.1-1988) standard. The tortured route that accomplishes this task for me is as follows:
datetime.datetime.strptime(datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S"),"%Y-%m-%d %H:%M:%S")
Is there a simpler way to end up with a datetime.datetime object for mongoDB than this?
You can use datetime.replace() method -
>>> d = datetime.datetime.today().replace(microsecond=0)
>>> d
datetime.datetime(2015, 7, 18, 9, 50, 20)
This question already has answers here:
In Python, how do you convert seconds since epoch to a `datetime` object?
(5 answers)
Closed 8 years ago.
I would like to change the time from epoch time to a format readable by Kml extensions (such as iso format of time).
There is plenty of help to change from epoch to formats like YYYYMMDDHHMMSS and other structs using tuples and mktime, but to .iso formation , i haven't been able to find it.
utcfromtimestamp converts seconds since the epoch to the corresponding UTC datetime.datetime.
datetime.datetime objects have a isoformat method which returns the date as a string in ISO 8601 format.
In [6]: import datetime as DT
In [7]: seconds_since_epoch = 0
In [8]: DT.datetime.utcfromtimestamp(seconds_since_epoch)
Out[8]: datetime.datetime(1970, 1, 1, 0, 0)
In [9]: DT.datetime.utcfromtimestamp(seconds_since_epoch).isoformat()
Out[9]: '1970-01-01T00:00:00'