import time
from datetime import datetime
now = datetime.now()
print(f'当前时间: {now}')
print(f'当前时间拆分: {now.year, now.month, now.day}')
strf_now = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
print(f'格式化当前时间: {strf_now}')
strf_now_1 = datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')
print(f'格式化当前时间-1: {strf_now_1}')
<OUTPUT>
from datetime import timedelta
utc_now = datetime.utcnow()
print(f'UTC 当前时间: {utc_now}')
utc2local = utc_now + timedelta(hours=8)
print(f'当地当前时间: {utc2local}')
<OUTPUT>
from datetime import timedelta
delta_now = datetime.now() + timedelta(days=1)
print(f'delta_now: {delta_now}')
<OUTPUT>
delta_now: datetime.datetime(2021, 9, 8, 20, 54, 45, 479097)
import time
def time2stamp(t):
time_array = time.strptime(t, '%Y-%m-%d %H:%M:%S')
print(type(time_array), '->', time_array)
timestamp = time.mktime(time_array)
return timestamp
if __name__ == '__main__':
print('->', time2stamp('2020-12-10 23:34:30'))
<OUTPUT>
<class 'time.struct_time'> -> time.struct_time(tm_year=2020, tm_mon=12, tm_mday=10, tm_hour=23, tm_min=34, tm_sec=30, tm_wday=3, tm_yday=345, tm_isdst=-1)
-> 1607614470.0
import time
form datetime import datetime
def get_now_stamp():
now_time = datetime.now()
return time.mktime(now_time.timetuple())
if __name__ == '__main__':
print('->', get_now_stamp())
<OUTPUT>
-> 1607848078.0
from datetime import datetime
def stamp2time(timestamp):
return datetime.fromtimestamp(timestamp)
if __name__ == '__main__':
print('->', stamp2time(1607614470))
<OUTPUT>
-> datetime.datetime(2020, 12, 10, 23, 34, 30)
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |