How to convert numbers into dates in python? Let me introduce the relevant steps below:
Installation
There is no need to download the source code package to install manually, just use easy_install or pip to install online
easy_install python-dateutil
pip install python-dateutil
The dateutil module mainly has two functions, parser and rrule. Parser parses a string into datetime, while rrule generates datetime based on defined rules.
About parser
The string can be very arbitrary, you can use English words of time and date, and you can use horizontal lines, commas, spaces, etc. as delimiters.
The default is 0 o'clock if the time is not specified, today is the default if the date is not specified, and this year is the default if the year is not specified.
Related recommendations: "python video tutorial"
>>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("2013,08,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)#fuzzy开启模糊匹配,过滤掉无法识别的时间日期字符 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)
freq: can be understood as a unit. Can be YEARLY, MONTHLY, WEEKLY, DAILY, HOURLY, MINUTELY, SECONDLY. That is, year, month, day, week, hour, minute and second.
dtstart,until: is the start and end time.
wkst: Week start time.
interval: interval.
count: Specify how many to generate.
byxxx: Specify the matching period. For example, byweekday=(MO,TU) only matches Monday and Tuesday. byweekday can specify MO, TU, WE, TH, FR, SA, SU. That is Monday to Sunday.
>>from dateutil.rrule import * >>list(rrule(DAILY,dtstart=parse('2013-08-01'),until=parse('2013-08-07'))) #2013-08-01到2013-08-07每日 [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('2013-08-01'),until=parse('2013-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('2013-08-01'),until=parse('2013-08-07'))) #只生成3个 [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('2013-08-01'),until=parse('2013-08-07'))) #只匹配周一周二的 [datetime.datetime(2013, 8, 5, 0, 0), datetime.datetime(2013, 8, 6, 0, 0)] >>list(rrule(MONTHLY,dtstart=parse('2013-05-19'),until=parse('2013-08-20'))) #按月为单位 [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)]
Convert numbers to dates:
from dateutil.parser import parse a=20190613 b=str(a) c=parse(b)
print(c) 2019-06-13 00:00:00
The above is the detailed content of How to turn numbers into dates in Python. For more information, please follow other related articles on the PHP Chinese website!