如何将人类可读的日期文本('明天','星期一'等)转换为YYYY-MM-DD?

时间:2015-05-13 03:59:47

标签: python date date-conversion python-datetime python-dateutil

我正在编写一个Python函数/ class / module / gizmo来将以下值转换为YYYY-MM-DD。我不愿意接受使用非标准dateutil来实现更轻松的实施。

我不希望stackoverflow社区简单地“为我写这整件事”,我可能会在完成后回答我自己的问题。一路上,任何指针都非常感激。我还没有找到一个全面解决我的需求的Q + A讨论,但当然我会从那些解决部分内容的人那里借用。例如:"find Monday's""timedelta in years"以及"six months out"

另外:如果有一个现有的,成熟的库(即使它是一个非标准库)来处理一些/更多/所有这些,它可能会为我节省大量工作。谢谢你的帮助。

case-insensitive match and conversion of any of the following to YYYY-MM-DD
    today
    tomorrow
    yesterday
    monday     = next monday
    tuesday    = next tuesday
    wednesday  = next wednesday
    thursday   = next thursday
    friday     = next friday
    saturday   = next saturday
    sunday     = next sunday
    -monday    = last monday
    -tuesday   = last tuesday
    -wednesday = last wednesday
    -thursday  = last thursday
    -friday    = last friday
    -saturday  = last saturday
    -sunday    = last sunday
    x[d|w|m|y]  = 'x' number of days/weeks/months/years in future
    -x[d|w|m|y] = 'x' number of days/weeks/months/years in past
    examples
        1d  = 1 day from today
        2w  = 2 weeks from today
        3m  = 3 months from today
        4y  = 4 years from  today
        -4d = 4 days ago
        -3w = 3 weeks ago
        -2m = 2 months ago
        -1y = 1 year ago
 unique truncations of today, tomorrow, yesterday, and
 days of week (monday, tuesday) are matched with their 
 expanded values.  eg: 'mo', '-Tues', 'tom', 'yest', 'tod'

0 个答案:

没有答案