Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I generate a python timestamp to a particular format?

In python, how would I generate a timestamp to this specific format?

2010-03-20T10:33:22-07

I've searched high and low, but I couldn't find the correct term that describes generating this specific format.

like image 341
user3768071 Avatar asked Sep 20 '17 09:09

user3768071


People also ask

How do I print a date in a specific format in Python?

Use strftime() function of a datetime class The format codes are standard directives for mentioning in which format you want to represent datetime. For example, the %d-%m-%Y %H:%M:%S codes convert date to dd-mm-yyyy hh:mm:ss format.

How do I get the exact timestamp in Python?

To get the current time in particular, you can use the strftime() method and pass into it the string ”%H:%M:%S” representing hours, minutes, and seconds.


2 Answers

See the following example:

import datetime  now = datetime.datetime.now() now.strftime('%Y-%m-%dT%H:%M:%S') + ('-%02d' % (now.microsecond / 10000)) 

This could result in the following: '2017-09-20T11:52:32-98'

like image 81
Henk Dekker Avatar answered Oct 06 '22 01:10

Henk Dekker


You can use datetime with strftime. Exemple:

import datetime  date = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")  print(date) 

Will print:

2017-09-20T12:59:43.888955 
like image 30
Lame Fanello Avatar answered Oct 05 '22 23:10

Lame Fanello