2

What would be the equivalent of time.ctime() for UTC time?

Currently, if I enter time.ctime() in the interpreter, I get this:

'Mon Feb  5 17:24:48 2018'

This returns the local time and I would like to know how to get time in the same format except in UTC.

Edit: It's time.asctime(time.gmtime())

astro
  • 29
  • 3

4 Answers4

4

time.gmtime() returns an object representing the utctime.

To get it into the same format as time.ctime, you can use time.asctime().

>>> time.asctime(time.gmtime())
'Mon Feb  5 12:03:39 2018'
khelwood
  • 52,115
  • 13
  • 74
  • 94
1

This should work:

import datetime

utc = datetime.datetime.utcnow()
# datetime.datetime(2018, 2, 5, 12, 2, 56, 678723)

str(utc)
# '2018-02-05 12:05:10.617973'
jpp
  • 147,904
  • 31
  • 244
  • 302
1
import datetime
print(datetime.datetime.utcnow())

2018-02-05 12:05:53.329809

A_emperio
  • 246
  • 2
  • 14
1

Below is the code where you can select any timezone:

from datetime import datetime, timezone
utc_dt = datetime.now(timezone.utc) 
gB08
  • 182
  • 1
  • 10