Python time module datetime details
- 2020-05-30 20:29:55
- OfStack
The datetime module is used as a collection of date and time modules. datetime has two constants, MAXYEAR and MINYEAR, which are 9999 and 1, respectively.
The datetime module defines five classes, respectively
1.datetime.date: class for date
2.datetime.datetime: class for date and time
3.datetime.time: class representing time
4.datetime.timedelta: represents the time interval, that is, the interval between two time points
5.datetime.tzinfo: time zone information
1. First take a look at datetime.date class:
The date class has three parameters, datetime.date (year,month,day), which returns year-month-day
Methods:
1. datetime.date.ctime (), return format as Sun Apr 16 00:00:00 2017
2. datetime.date.fromtimestamp (timestamp), returns 1 date object according to the given lifetime; datetime.date.today () has the same effect
3. datetime.date.isocalendar (): returns tuples in formats such as (year, month, day),(2017, 15, 6)
4. datetime.date.isoformat () : returns the format as YYYY-MM-DD
5. datetime.date.isoweekday () : returns the week (0-6) for a given date, week 1=0, Sunday =6
6. datetime.date.replace (year,month,day) : replaces a given date, but does not change the original date
7. datetime.date.strftime (format): format the date and time according to the given format.
8. datetime. date. timetuple () : returns the date corresponding time. struct_time object
time.struct_time(tm_year=2017, tm_mon=4, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=105, tm_isdst=-1)
9. datetime. date. weekday () : returns the date of the week
Time and date formatting symbols in python:
%y two-digit years are (00-99)
%Y 4-digit years (000-9999)
%m (01-12)
%d 1 day in a month (0-31)
%H 24-hour system hours (0-23)
%I 12-hour system (01-12)
%M minutes (00=59)
%S seconds (00-59)
Simplify week names locally
%A local full week name
%b locally simplified month names
%B local full month name
%c local corresponding date and time representations
%j 1 day of the year (001-366)
%p local A.M. Or P.M
The number of weeks in a year (00-53) Sunday is the beginning of the week
w week (0-6) begins on Sunday
Week 1 is the beginning of the week
%x local corresponding date representation
%X local corresponding time representation
%Z name of the current time zone
The %% % sign itself
2. Take a look at the time class for datetime
time class has five arguments, datetime time (hour minute, second, microsecond, tzoninfo), returns 08:29:30
1.datetime.time.replace()
2. datetime. time. strftime (format) : according to the format format the return time
3. datetime. time. tzname () : returns the time zone name
4. datetime. time. utcoffset () : returns the time zone offset time
3. datetime class of datetime
The datetime class has many parameters, datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]), which returns the day, month, year, hour, minute, and second
datetime.datetime.ctime()
datetime. datetime. now().date() : returns the date part of the current date time
datetime.datetime.now ().time () : returns the time portion of the current date and time
datetime.datetime.fromtimestamp()
datetime.datetime.now () : returns the current system time
datetime.datetime.replace()
datetime.datetime.strftime () : converted from date format to string format
datetime.datetime.now().strftime('%b-%d-%Y %H:%M:%S')
'Apr-16-2017 21:01:35'
datetime.datetime.strptime (): converted from string format to date format
datetime.datetime.strptime('Apr-16-2017 21:01:35', '%b-%d-%Y %H:%M:%S')
2017-04-16 21:01:35
4. timedelta class of datetime
datetime.datetime.timedelta is used to calculate the difference between two dates, for example:
>>> a=datetime.datetime.now()
>>> b=datetime.datetime.now()
>>> a
datetime.datetime(2017, 4, 16, 21, 21, 20, 871000)
>>> b
datetime.datetime(2017, 4, 16, 21, 21, 29, 603000)
>>> b-a
datetime.timedelta(0, 8, 732000)
>>> (b-a).seconds
8
or
time1 = datetime.datetime(2016, 10, 20)
time2 = datetime.datetime(2015, 11, 2)
""" Calculate the day difference """
print(time1-time2).days
""" Count the number of seconds between the two dates """
print (time1-time2).total_seconds()