简体   繁体   English

如何将日期时间与当前日期进行比较?

[英]How to compare datetime with current date?

I have the following with me我有以下物品

    >>> import datetime 
    >>>
    >>> some_date = datetime.datetime.strptime('2017-07-03T20:35:45.000Z', '%Y-%m-%dT%H:%M:%S.%fZ') 
    >>> some_date 
    datetime.datetime(2017, 7, 3, 20, 35, 45) 
    >>> datetime.datetime.now() 
    datetime.datetime(2017, 8, 21, 22, 4, 20, 215391) 
    >>>

I wanted to find out whether some_date is N days older than today or not?我想知道some_date是否比今天大 N 天? Like lets' take N as 2. Then I wanted to find whether some_date came two days ago or not.比如让我们把 N 取为 2。然后我想知道 some_date 是否在两天前出现。 How should I subtract both?我应该如何减去两者? Would this work?这行得通吗? https://stackoverflow.com/a/441152/3834059 https://stackoverflow.com/a/441152/3834059

you can check it like this你可以这样检查
you dont need to str time你不需要时间

import datetime

if my_time == datetime.timedelta(days=2):
    print("my time is for 2 days ago")

or another examples for past times或过去的其他例子

import datetime

if my_time >= datetime.datetime.now() - datetime.timedelta(days=1):
    print("my time is in past")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我将如何存储日期时间,然后将日期时间与当前日期时间进行比较? - How would I store a datetime, then compare the datetime against current datetime? Python:将对象列转换为日期时间并与当前日期进行比较 - Python: convert object column into datetime and compare with current date 如何比较 Python 中的日期和日期时间? - How can I compare a date and a datetime in Python? 比较 Django 中的日期和日期时间 - Compare date and datetime in Django 如何比较当前日期和过去的日期? - How to compare current date with past epoch dates? 如何将当前 UTC 日期时间与仅最后三个铸币厂数据进行比较? - How to compare the current UTC datetime with only the last three mint data? 如何仅将模型的日期时间字段中的日期与当前日期进行比较? - how to compare only date from a model's datetimefield with current date? 熊猫数据框如何比较日期时间而忽略日期 - pandas dataframe how to compare datetime while ignoring the date 如何比较 python 中的日期? 我有月份日期年份格式并想与当前日期(7 天内)进行比较 - How to compare date in python? I have a month date year format and want to compare to the current date (within 7 days) 如何创建日期时间列表并检查它是否与当前日期时间匹配,然后打印当前日期时间? - How to create a list of date time and check if it matches with current date time, then print current datetime?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM