Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 How to format to yyyy-mm-ddThh:mm:ssZ

I'm new to Python and I cannot for the life of me find my specific answer online. I need to format a timestamp to this exact format to include 'T', 'Z' and no sub or miliseconds like this yyyy-mm-ddThh:mm:ssZ i.e. 2019-03-06T11:22:00Z. There's lots of stuff on parsing this format but nothing about formatting this way. The only way I have nearly got it to work involves sub-seconds which I do not need. I've tried using arrow and reading their documentation but unable to get anything to work. Any help would be appreciated.

like image 987
ra67052 Avatar asked Mar 06 '19 11:03

ra67052


People also ask

What is T and Z in timestamp Python?

Convert Timestamp to Datetime (format) This looks something like the below value with the T and Z alphabets. 2014-09-12T19:34:29Z. Here the alphabet T stands for Time and Z stands for the Zero timezone which represents the offset from the coordinated universal time(UTC).

How do I convert a string to a datetime in Python?

To convert string to datetime in Python, use the strptime() method. The strptime() is a built-in function of the Python datetime class used to convert a string representation of the​ date/time to a date object.

What means Z at the end of timestamp?

The Z stands for the Zero timezone, as it is offset by 0 from the Coordinated Universal Time (UTC). Both characters are just static letters in the format, which is why they are not documented by the datetime.


1 Answers

Try datetime library

import datetime

output_date = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")
print(output_date)

For more information, refer to the Python Documentation.

like image 162
skaul05 Avatar answered Nov 14 '22 03:11

skaul05