猜测python的日期格式

2024-03-28 14:57:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python模块中编写了一个方法,它试图让用户更轻松地生活。此方法实现在该日历中创建事件。在

def update_event(start_datetime=None, end_datetime=None, description=None):
'''
Args:
  start_date: string datetime in the format YYYY-MM-DD or in RFC 3339
  end_date: string datetime in the format YYYY-MM-DD or in RFC 3339
  description: string with description (\n are transforrmed into new lines)
'''

如果用户指定开始日期或结束日期,则应进行检查,以确定日期是采用YYYY-MM-DD格式还是datetime RFC 3339格式。在

^{pr2}$

这是个好办法吗?我能以更好的方式检查一下我收到的是什么样的约会吗? 谢谢!在


Tags: the方法用户innonedatetimedatestring
1条回答
网友
1楼 · 发布于 2024-03-28 14:57:45

^{}可用于尝试将字符串解析为datetime对象。在

from dateutil.parser import parse

def update_event(start_datetime=None, end_datetime=None, description=None):
    if start_datetime is not None:
        new_start_time = parse(start_datetime)

        return new_start_time

d = ['23/04/2014', '24-04-2013', '25th April 2014']

new = [update_event(i) for i in d]

for date in new:
    print(date)
    # 2014-04-23 00:00:00
    # 2013-04-24 00:00:00
    # 2014-04-25 00:00:00

相关问题 更多 >