In dateutil, there are two items that attract me, one is parser and the other is rrule.
Parser parses the string into datetime, while rrule generates datetime according to the defined rules.
Install
You do not need to download the source code package for manual installation. You can directly use easy_install or pip for online installation.
Easy_install Python-dateutil
PIP install Python-dateutil
About parser
Strings can be casual. You can use English words of time and date, and use hyphens, commas, and spaces as separators.
If no time is specified, the default value is 0. if no date is specified, the default value is today. If no year is specified, the default value is this year.
> From dateutil. parser import parse
> Parse ("wed, Nov 12 ")
Datetime. datetime (2013, 11, 12, 0, 0)
> Parse ("2013-08-20 ")
Datetime. datetime (2013, 8, 20, 0, 0)
> Parse ("20130820 ")
Datetime. datetime (2013, 8, 20, 0, 0)
> Parse ("Maid, 20 ")
Datetime. datetime (2013, 8, 20, 0, 0)
> Parse ("08,20 ")
Datetime. datetime (2013, 8, 20, 0, 0)
> Parse ("12:00:00 ")
Datetime. datetime (2013, 8, 20, 12, 0)
> Parse ("this is the wonderful moment 12:00:00, I feel good", fuzzy = true) # enable fuzzy match to filter out unrecognized time and date characters
Datetime. datetime (2013, 8, 20, 12, 0)
About rrule
The function prototype is as follows.
Rrule (self, freq, dtstart = none, interval = 1, wkst = none, Count = none, until = none, bysetpos = none,
Bymonth = none, bymonthday = none, byyearday = none, byeaster = none, byweekno = none, byweekday = none, byhour = none, byminute = none, bysecond = none, cache = false)
Where
Freq: unit. It can be yearly, monthly, weekly, daily, hourly, minutely, and secondly. That is, year, month, day, week, minute, and second.
Dtstart, until: the start time and end time.
Wkst: the start time of the week.
Interval: interval.
Count: number of instances generated.
Byxxx: Specifies the matching period. For example, byweekday = (Mo, tu) only matches Monday and Tuesday. Byweekday can specify Mo, tu, we, Th, FR, SA, Su. Monday to Sunday.
> From dateutil. rrule import *
> List (rrule (daily, dtstart = parse ('2017-08-01 '), until = parse ('2017-08-07') #2013 to 2013 daily
[Datetime. datetime (2013, 8, 1, 0, 0 ),
Datetime. datetime (2013, 8, 2, 0, 0 ),
Datetime. datetime (2013, 8, 3, 0, 0 ),
Datetime. datetime (2013, 8, 4, 0, 0 ),
Datetime. datetime (2013, 8, 5, 0, 0 ),
Datetime. datetime (2013, 8, 6, 0, 0 ),
Datetime. datetime (2013, 8, 7, 0, 0)]
> List (rrule (daily, interval = 3, dtstart = parse ('2017-08-01 '), until = parse ('2017-08-07') #3
[Datetime. datetime (2013, 8, 1, 0, 0 ),
Datetime. datetime (2013, 8, 4, 0, 0 ),
Datetime. datetime (2013, 8, 7, 0, 0)]
> List (rrule (daily, Count = 3, dtstart = parse ('2017-08-01 '), until = parse ('2017-08-07') # generate only three
[Datetime. datetime (2013, 8, 1, 0, 0 ),
Datetime. datetime (2013, 8, 2, 0, 0 ),
Datetime. datetime (2013, 8, 3, 0, 0)]
> List (rrule (daily, byweekday = (Mo, tu), dtstart = parse ('2017-08-01 '), until = parse ('2017-08-07 '))) # match only Monday and Tuesday
[Datetime. datetime (2013, 8, 5, 0, 0), datetime. datetime (2013, 8, 6, 0, 0)]
> List (rrule (monthly, dtstart = parse ('2017-05-19 '), until = parse ('2017-08-20') # monthly
[Datetime. datetime (2013, 5, 19, 0, 0 ),
Datetime. datetime (2013, 6, 19, 0, 0 ),
Datetime. datetime (2013, 7, 19, 0, 0 ),
Datetime. datetime (2013, 8, 19, 0, 0)]