Python gets timestamp

Use the datetime library to get the time.

Get the current time:

import datetime
print(datetime.datetime.now())

. What follows is microseconds, which is also a unit of time. 1 second = 1,000,000 microseconds.

Convert to timestamp:

import datetime

date = datetime.datetime.now()
timestamp = date.timestamp()
print(timestamp)

Get the ten-digit timestamp:

import datetime

date = datetime.datetime.now()
timestamp = int(date.timestamp())
print(timestamp)

Get thirteen timestamps:

import datetime

date = datetime.datetime.now()
timestamp = int(date.timestamp()*1000)
print(timestamp)

Get the timestamp of a specific time (for example, get the timestamp of 00:00:00):

import datetime

date = datetime.datetime.now()
begin_time = date.replace(hour=0, minute=0, second=0, microsecond=0)
timestamp = int(date.timestamp())
print(timestamp)

        The input parameters of date.replace() can replace the value in the current date, year, month, day, hour, minute, second, microsecond

        To get the tenth digit, you only need to replace hour, minute, and second. If you want to get the thirteenth digit, you also need to replace microsecond. Otherwise, the microsecond of the current time will be obtained.

        Get thirteen without replacing microsecond:

        Get thirteen replacement microseconds:

(Get the timestamp at 23:59:59): Microsecond also needs to be replaced when getting thirteen

import datetime

date = datetime.datetime.now()
begin_time = date.replace(hour=0, minute=0, second=0, microsecond=0)
timestamp = int(begin_time.timestamp() * 1000)
end_time = date.replace(hour=23, minute=59, second=59, microsecond=999999)
end_timestamp = int(end_time.timestamp() * 1000)
print(timestamp)
print(end_timestamp)

        Replace microsecond with 999999 

Guess you like

Origin blog.csdn.net/h360583690/article/details/133563101