如何将 'day/month' 字符串转换为 python 中的日期并将其与 Odoo 日期字段进行比较?
How to convert a 'day/month' string to date in python and compare it with an Odoo date field?
我有一个 day/month
字符串,我想将该字符串转换为日期对象并将该月的最后一天与另一个日期进行比较
示例:
对于 08/2021
(2021 年 8 月),我想将该月的最后一天 (31-08-2021) 与另一个日期(日期字段)进行比较,
对于02/2020
我比较什么29-02-2020
对于02/2021
我比较什么28-02-2020
此示例向您展示如何将“02/2020”转换为 Pythondatetime
以及如何获取该月的最后一天。您可以使用它来将结果与另一个 datetime
:
import datetime
from dateutil.relativedelta import relativedelta
date = '02/2020'
last_day = datetime.datetime.strptime(date, '%m/%Y') + relativedelta(day=31)
# last_day will be a datetime of the last day of the month which you can use to compare against another datetime
在这个例子中,结果是 datetime.datetime(2020, 2, 29, 0, 0)
因为 2020 年是闰年
如果您不想添加 dateutil,可以使用 calendar.monthrange
查找该月的最后一天。
import calendar
from datetime import datetime
def get_last_day_date(year_month_str):
date = datetime.strptime(year_month_str, "%m/%Y")
last_day = calendar.monthrange(date.year, date.month)[1]
return datetime(date.year, date.month, last_day)
get_last_day_date("08/2020")
# datetime.datetime(2020, 8, 31, 0, 0)
b='08/2021'
a=b.split('/')
import calendar
import datetime
z=(str(calendar.monthrange(int(a[1]),int(a[0]))[1])+'-'+b.replace('/','-'))
d=datetime.datetime.strptime(z,'%d-%m-%Y').date()
print(d)
n=datetime.date.today()
print(n)
n<d
输出:
2021-08-31
2021-01-28
True
只需 importing/using datetime
库即可完成,您可以在此处了解具体方法。
通过将字符串日期传递给方法。
import datetime
def convert_string_to_datetime(self, datetime_in_string):
datetime_in_string = str(datetime_in_string)
datetime_format = "%Y-%m-%d %H:%M:%S"
datetime_in_datetime_format = datetime.datetime.strptime(datetime_in_string, datetime_format)
return datetime_in_datetime_format
new_datetime_field = convert_string_to_datetime(datetime_in_string)
通过单行修改with
import datetime
new_datetime_field = datetime.datetime.strptime(YOUR_DATETIME_IN_STRING, "%Y-%m-%d %H:%M:%S")
转换成datetime
后可以比较了。
if new_datetime_field > odoo_datetime_field:
pass
我有一个 day/month
字符串,我想将该字符串转换为日期对象并将该月的最后一天与另一个日期进行比较
示例:
对于 08/2021
(2021 年 8 月),我想将该月的最后一天 (31-08-2021) 与另一个日期(日期字段)进行比较,
对于 对于02/2020
我比较什么29-02-202002/2021
我比较什么28-02-2020
此示例向您展示如何将“02/2020”转换为 Pythondatetime
以及如何获取该月的最后一天。您可以使用它来将结果与另一个 datetime
:
import datetime
from dateutil.relativedelta import relativedelta
date = '02/2020'
last_day = datetime.datetime.strptime(date, '%m/%Y') + relativedelta(day=31)
# last_day will be a datetime of the last day of the month which you can use to compare against another datetime
在这个例子中,结果是 datetime.datetime(2020, 2, 29, 0, 0)
因为 2020 年是闰年
如果您不想添加 dateutil,可以使用 calendar.monthrange
查找该月的最后一天。
import calendar
from datetime import datetime
def get_last_day_date(year_month_str):
date = datetime.strptime(year_month_str, "%m/%Y")
last_day = calendar.monthrange(date.year, date.month)[1]
return datetime(date.year, date.month, last_day)
get_last_day_date("08/2020")
# datetime.datetime(2020, 8, 31, 0, 0)
b='08/2021'
a=b.split('/')
import calendar
import datetime
z=(str(calendar.monthrange(int(a[1]),int(a[0]))[1])+'-'+b.replace('/','-'))
d=datetime.datetime.strptime(z,'%d-%m-%Y').date()
print(d)
n=datetime.date.today()
print(n)
n<d
输出:
2021-08-31
2021-01-28
True
只需 importing/using datetime
库即可完成,您可以在此处了解具体方法。
通过将字符串日期传递给方法。
import datetime
def convert_string_to_datetime(self, datetime_in_string):
datetime_in_string = str(datetime_in_string)
datetime_format = "%Y-%m-%d %H:%M:%S"
datetime_in_datetime_format = datetime.datetime.strptime(datetime_in_string, datetime_format)
return datetime_in_datetime_format
new_datetime_field = convert_string_to_datetime(datetime_in_string)
通过单行修改with
import datetime
new_datetime_field = datetime.datetime.strptime(YOUR_DATETIME_IN_STRING, "%Y-%m-%d %H:%M:%S")
转换成datetime
后可以比较了。
if new_datetime_field > odoo_datetime_field:
pass