输入日期格式字符串 获取时间戳 构造时间,时间加减等
import time
import datetime
def format_time() -> None:
“”"
日期格式化
“”"
inputStr1: str = input(“输入一个时间格式的字符串”)
timeArray = time.strptime(inputStr1, ‘%Y-%m-%d %H:%M:%S’)
# 得到的是 float毫秒数
intnumber = int(time.mktime(timeArray))
timeArray = time.strptime(inputStr1, '%Y-%m-%d %H:%M:%S')
str1 = time.strftime('%Y-%m-%d %H-%M-%S',timeArray)
print(str1)
def three_days_age_time1() -> None:
“”"
获取三天前时间
“”"
threeDaysAgoDateTime = datetime.datetime.now() - datetime.timedelta(days=3)
timestamp1 = int(time.mktime(threeDaysAgoDateTime.timetuple()))
# 格式直接转
otherFormatStr = threeDaysAgoDateTime.strftime('%Y-%m-%d %H:%M:%S')
print(otherFormatStr)
# 时间戳构造
dataArrary = datetime.datetime.utcfromtimestamp(timestamp1)
print(dataArrary)
def timestamp_to_datetime() -> None:
“”"
时间戳转日期字符串等
“”"
nowTime = int(time.time())
structTime = time.localtime(nowTime)
otherTime = time.strftime(‘%Y-%m-%d %H:%M:%S’, structTime)
print(otherTime)
time.sleep(1)
nowTime2 = datetime.datetime.now()
str1 = nowTime2.strftime('%Y-%m-%d %H:%M:%S')
print(str1)
time.sleep(1)
nowTime = time.localtime(1557502800)
otherTime = time.strftime('%Y-%m-%d %H:%M:%S', nowTime)
print(otherTime)
dateArray = datetime.datetime.utcfromtimestamp(1557502900)
datetimeFormatStr = dateArray.strftime('%Y-%m-%d %H:%M:%S')
print(datetimeFormatStr)