如何使用python检查日期为空? [英] How to check the date is empty using python?

查看:1096
本文介绍了如何使用python检查日期为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

startdateenddate的值从jenkin到lambda. 在这段代码中,我尝试使用event["start_date"] and event["end_date"]获取值,并且可以正常工作,但是如果startdateenddate不可用,则代码应自动采用今天和昨天的日期.

The startdate and enddate values are coming from jenkin to lambda. In this code I am trying to get value using event["start_date"] and event["end_date"] and it's working fine, but if startdate and enddate are not available then the code should automatically take today's and yesterday's date.

我是python的新手,有人可以帮忙吗?

I am new to python, can someone help here?

我尝试如下进行操作,但出现错误. 如果我要提到"startdate和enddate为none"并将两个值都设置为none,则它可以正常工作,但是我需要在代码中实现提及的内容,因为我正在将AWS lambda与python结合使用.

I tried as below but I am getting error. If I will mention 'startdate and enddate is none' and set both values as none then it's working but I need to implement mentioned in code as I am using AWS lambda with python.

import datetime
from datetime import timedelta

dateformat = "%Y-%m-%d"
startdate = datetime.datetime.strptime("", dateformat)
enddate = datetime.datetime.strptime("", dateformat)
# Both startdate and enddate values come from Jenkin to lambda  - 
event["start_date"] and event["end_date"]
if (startdate and enddate) == "":
    startdate = datetime.date.today()
    enddate = startdate - datetime.timedelta(days = 1)
    print('yesterday : ', enddate)  
    print('Today : ', startdate)

当前错误输出:

Traceback (most recent call last):
  File "variable_value_change.py", line 10, in <module>
    startdate = datetime.datetime.strptime("", dateformat)
  File "C:\Users\336635743\AppData\Local\Programs\Python\Python37-32\lib\_strptime.py", line 577, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "C:\Users\336635743\AppData\Local\Programs\Python\Python37-32\lib\_strptime.py", line 359, in _strptime
   (data_string, format))
ValueError: time data '' does not match format '%Y-%m-%d'

推荐答案

更改此行

if (startdate and enddate) == "":

if startdate == "" and enddate == "":

另一种方式:

if not startdate and not enddate: # PEP 8 recommended

这篇关于如何使用python检查日期为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆