作者:柯怡如63391 | 来源:互联网 | 2023-07-31 14:26
#获取用户的timezone小时,分钟hours=re.match(rGMT([+|-][\d]{1,2}):\d{1}\d{1},timezone).group(1)
# 获取用户的timezone小时,分钟
hours = re.match(r'GMT([+|-][\d]{1,2}):\d{1}\d{1}',timezone).group(1)
minutes = re.match(r'GMT([+|-][\d]{1,2}):\d{1}\d{1}',timezone).group().split(":")[1]
hours, minutes = int(hours), int(minutes)
minutes = minutes if hours = 0 else -minutes
print(hours, minutes)
# 8 0
# 获取用户所在的今天时间,昨天时间(datetime格式)
now_time = datetime.utcnow()+timedelta(hours=hours,minutes=minutes)
yseterday_time = now_time + timedelta(days=-1)
print(type(now_time), now_time)
# class 'datetime.datetime' 2020-11-09 00:47:49.047901
print(type(yseterday_time), yseterday_time)
# class 'datetime.datetime' 2020-11-08 00:47:49.047901
2.将时间转化成字符串时间
# now_time, yesterday_time接上步骤
# 将今天时间,昨天时间转化成字符串格式时间
str_now_time = now_time.strftime('%Y-%m-%d')
str_yesterday_time = yseterday_time.strftime('%Y-%m-%d')
print(type(str_now_time), str_now_time)
# class 'str' 2020-11-09
print(type(str_yesterday_time), str_yesterday_time)
# class 'str' 2020-11-08
3.将字符串时间转化为毫秒时间戳
str_time = "2020-11-9"
# 先将字符串时间转化为时间数组
timeArray = time.strptime(str_time, "%Y-%m-%d")
print(timeArray)
# time.struct_time(tm_year=2020, tm_mon=11, tm_mday=9, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=314, tm_isdst=-1)
# 将时间数组转化为毫秒时间戳
timeStamp = int(time.mktime(timeArray))*1000
print(timeStamp)
# 1604851200000
文章来源于https://www.cnblogs.com/dalyday/p/13946680.html