写了一个火车头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))