user3768071
user3768071

Reputation: 887

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.

Upvotes: 49

Views: 81522

Answers (2)

Lame Fanello
Lame Fanello

Reputation: 615

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

Upvotes: 33

Henk Dekker
Henk Dekker

Reputation: 716

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'

Upvotes: 60

Related Questions