时,天,周,月,年日期转换案例

# -*- coding:utf-8 -*-
# @Time : 2022/1/28 13:38
# @Author : huoyu
# @File : data_times.py
# @Software: PyCharm

import re
import time


def get_times(Upload_time):
    hous = re.compile('.*?(\d+)小时前', re.S)
    days = re.compile('.*?(\d+)天前', re.S)
    weeks = re.compile('.*?(\d+)周前', re.S)
    months = re.compile('.*?(\d+)个月前', re.S)
    years = re.compile('.*?(\d+)年前', re.S)

    countent=1
    current_time=time.time()
    current_time=current_time*1000
    while True:
        # 小时
        result_h = re.findall(hous, Upload_time)
        if result_h!=[]:
            result_h=int(result_h[0])
            result_h=result_h*3600000
            result_h=current_time-result_h
            countent=result_h
            break
        result_d = re.findall(days, Upload_time)
        if result_d != []:
            result_d = int(result_d[0])
            result_d = result_d * 86400000
            result_d = current_time - result_d
            countent = result_d
            break
        result_w = re.findall(weeks, Upload_time)
        if result_w!=[]:
            result_w = int(result_w[0])
            result_w = result_w * 60480000
            result_w = current_time - result_w
            countent = result_w
            break
        result_m = re.findall(months, Upload_time)
        if result_m!=[]:
            result_m = int(result_m[0])
            result_m = result_m * 2592000000
            result_m=current_time-result_m
            countent = result_m
            break
        result_y = re.findall(years, Upload_time)
        if result_y != []:
            result_y = int(result_y[0])
            result_y = result_y * 31104000000
            result_y = current_time - result_y
            countent = result_y
            break
    countent=int(countent)
    print(countent)
    return countent

if __name__ == '__main__':
    times = ["1小时前", "1天前", "直播时间:3小时前", "直播时间:1天前", "4周前", "2个月前", "直播时间:2个月前"]
    for i in times:
        get_times(i)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

火浴R

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值