Hướng dẫn get datetime utc python

How do I get the UTC time, i.e. milliseconds since Unix epoch on Jan 1, 1970?

Mateen Ulhaq

22.2k16 gold badges86 silver badges127 bronze badges

asked Apr 11, 2013 at 3:33

James ClarkeJames Clarke

2,1072 gold badges12 silver badges3 bronze badges

3

Use datetime.utcnow[]:

from datetime import datetime
datetime.utcnow[]

For your purposes when you need to calculate an amount of time spent between two dates all that you need is to subtract end and start dates. The results of such subtraction is a timedelta object.

From the python docs:

class datetime.timedelta[[days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]]]

And this means that by default you can get any of the fields mentioned in it's definition - days, seconds, microseconds, milliseconds, minutes, hours, weeks. Also timedelta instance has total_seconds[] method that:

Return the total number of seconds contained in the duration. Equivalent to [td.microseconds + [td.seconds + td.days * 24 * 3600] * 106] / 106 computed with true division enabled.

Mateen Ulhaq

22.2k16 gold badges86 silver badges127 bronze badges

answered Apr 11, 2013 at 3:35

Artsiom RudzenkaArtsiom Rudzenka

27k4 gold badges32 silver badges51 bronze badges

2

Timezone-aware datetime object, unlike datetime.utcnow[]:

from datetime import datetime,timezone
now_utc = datetime.now[timezone.utc]

Timestamp in milliseconds since Unix epoch:

datetime.now[timezone.utc].timestamp[] * 1000

Mateen Ulhaq

22.2k16 gold badges86 silver badges127 bronze badges

answered Oct 8, 2019 at 22:34

Tim RichardsonTim Richardson

5,6176 gold badges42 silver badges63 bronze badges

4

In the form closest to your original:

import datetime

def UtcNow[]:
    now = datetime.datetime.utcnow[]
    return now

If you need to know the number of seconds from 1970-01-01 rather than a native Python datetime, use this instead:

return [now - datetime.datetime[1970, 1, 1]].total_seconds[]

Python has naming conventions that are at odds with what you might be used to in Javascript, see PEP 8. Also, a function that simply returns the result of another function is rather silly; if it's just a matter of making it more accessible, you can create another name for a function by simply assigning it. The first example above could be replaced with:

utc_now = datetime.datetime.utcnow

answered Apr 11, 2013 at 3:36

Mark RansomMark Ransom

290k40 gold badges383 silver badges606 bronze badges

13

import datetime
import pytz

# datetime object with timezone awareness:
datetime.datetime.now[tz=pytz.utc]

# seconds from epoch:
datetime.datetime.now[tz=pytz.utc].timestamp[] 

# ms from epoch:
int[datetime.datetime.now[tz=pytz.utc].timestamp[] * 1000] 

answered Sep 3, 2018 at 8:56

1

Timezone aware with zero external dependencies:

from datetime import datetime, timezone

def utc_now[]:
    return datetime.utcnow[].replace[tzinfo=timezone.utc]

answered Feb 25, 2019 at 16:05

Cesar CanassaCesar Canassa

17k9 gold badges64 silver badges68 bronze badges

3

From datetime.datetime you already can export to timestamps with method strftime. Following your function example:

import datetime
def UtcNow[]:
    now = datetime.datetime.utcnow[]
    return int[now.strftime["%s"]]

If you want microseconds, you need to change the export string and cast to float like: return float[now.strftime["%s.%f"]]

answered Mar 21, 2017 at 17:19

hectorcantohectorcanto

1,76713 silver badges17 bronze badges

3

you could use datetime library to get UTC time even local time.

import datetime

utc_time = datetime.datetime.utcnow[] 
print[utc_time.strftime['%Y%m%d %H%M%S']]

answered Feb 19, 2021 at 6:41

HCHOHCHO

951 silver badge5 bronze badges

why all reply based on datetime and not time? i think is the easy way !

import time
nowgmt = time.strftime["%Y-%m-%d %H:%M:%S", time.gmtime[]]
print[nowgmt]

answered Aug 28, 2021 at 14:14

1

Chủ Đề