Python 检测目标日期是否为当天?

微信扫一扫,分享到朋友圈

Python 检测目标日期是否为当天?
收藏 00

写了一个火车头python插件,用于判断目标站点的文章是否是新文章。

判断时间是否为今天的日期

def is_today(target_date):
    """
    2020-03-25 17:03:55
    Detects if the date is current date
    :param target_date:
    :return: Boolean
    """
    # Get the year, month and day
    c_year = datetime.datetime.now().year
    c_month = datetime.datetime.now().month
    c_day = datetime.datetime.now().day

    # Disassemble the date
    date_list = target_date.split(" ")[0].split("-")
    t_year = int(date_list[0])
    t_month = int(date_list[1])
    t_day = int(date_list[2])

    final = False
    # Compare years, months and days
    if c_year == t_year and c_month == t_month and c_day == t_day:
        final = True

    return final

获取当前时间

import time
now_time = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time()))

2个时间作比较

import datetime
import time
from datetime import datetime, date
now_time = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time()))
art_time = '2022-10-01 08:42'

time_1_struct = datetime.strptime(now_time, "%Y-%m-%d %H:%M")
time_2_struct = datetime.strptime(art_time, "%Y-%m-%d %H:%M")
total_seconds = (time_1_struct - time_2_struct).total_seconds()
hours=total_seconds/3600
print(int(hours))

 

 

一个热爱互联网的咸鱼
下一篇

js实现网站引入天气预报功能

你也可能喜欢

发表评论

您的电子邮件地址不会被公开。 必填项已用 * 标注

提示:点击验证后方可评论!

插入图片

热门

    抱歉,30天内未发布文章!
返回顶部