1、 Module import
# Date time object , Common methods are date(), time() >>> from datetime import datetime # Date object , Common attributes are year, month, day >>> from datetime import date # Time object , Common attributes are hour, minute, second, microsecond, tzinfo( The time zone ) >>> from datetime import time # The time interval , Common attributes are weeks, days, hours, ... >>> from datetime import timedelta # Date interval , Common attributes are years, months, weeks, days, hours ... >>> from dateutil import relativedelta Be careful : This module needs to import (pip install python-dateutil)
2、 Initialize object
>>> datetime_obj = datetime.today() >>> date_obj = date.today() >>> str_obj = '2020-11-10 15:42:53' >>> timestamp_obj = '1604994173'
3、 format conversion
# datetime turn string ( Use it directly str Forced conversion is enough ) >>> str(datetime_obj) '2020-11-10 15:42:53.821000' >>> str(date_obj) '2020-11-10' # datetime Format as string >>> datetime_obj.strftime('%Y-%m-%d %H:%M:%S') '2020-11-10 15:42:53' >>> datet_obj.strftime('%Y-%m-%d') '2020-11-10' # datetime turn date >>> datetime_obj.date() datetime.date(2012,11,19) # datetime turn timestamp >>> datetime_obj.timestamp() 1604994173.821 # Be careful :Python The timestamp is in seconds ,java Timestamps are in milliseconds # string turn datetime >>> datetime.strptime(str_obj,'%Y-%m-%d %H:%M:%S') datetime.datetime(2020, 11, 10, 15, 42, 53) # timestamp turn datetime >>> datetime.fromtimestamp(timestamp_obj) datetime.datetime(2020, 11, 10, 15, 42, 53) # date turn datetime >>> datetime.strptime(str(date_obj), '%Y-%m-%d') datetime.datetime(2020, 11, 10, 0, 0)
4、 Type judgment
>>> isinstance(datetime_obj, date) True >>> isinstance(datetime_obj, datetime) True >>> isinstance(date_obj, date) True >>> isinstance(date_obj, datetime) False
5、 Date time calculation
# Years plus 1 >>> date_obj + relativedelta.relativedelta(years=1) datetime.date(2021, 11, 10) # Monthly limit reduction 1 >>> date_obj - relativedelta.relativedelta(months=1) datetime.date(2020, 10, 10) >>> datetime_obj + relativedelta.relativedelta(months=-1) datetime.datetime(2020, 10, 10, 15, 42, 53, 821000) # Week plus and minus 1 >>> date_obj + relativedelta.relativedelta(weeks=-1) datetime.date(2020, 11, 3) >>> date_obj + datetime.timedelta(weeks=1) datetime.date(2020, 11, 17) # Days minus 1 >>> date_obj - relativedelta.relativedelta(days=1) datetime.date(2020, 11, 9)
# Plus hours 1 >>> datetime_obj + datetime.timedelta(hours=1) datetime.datetime(2020, 11, 10, 16, 42, 53, 821000)
# minute (minutes)、 second (seconds)、 millisecond (microseconds) And so on
6、 Calendar module
>>> import calendar >>> calendar.month(2018, 12) December 2018 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 # Set the week to start on Wednesday >>> calendar.setfirstweekday(2)
# Determine if it's a leap year >>> print(calendar.isleap(2018)) False
# Returns the day of the week and the number of days of the month on the first day of the month >>> print(calendar.monthrange(2018, 12)) (5, 31)
Python Medium time date format symbol :
------------------------------------
%y Two digit year representation (00-99)
%Y Four digit year representation (000-9999)
%m month (01-12)
%d One day in the month (0-31)
%H 24 Hours in hours (0-23)
%I 12 Hours in hours (01-12)
%M Minutes (00=59)
%S second (00-59)
%a Local simplified week name
%A Local full week name
%b Local simplified month name
%B Local full month name
%c Local corresponding date and time representation
%j One day in the year (001-366)
%p Local A.M. or P.M. The equivalent of
%U Weeks of the year (00-53) Sunday is the beginning of the week
%w week (0-6), Sunday is the beginning of the week
%W Weeks of the year (00-53) Monday is the beginning of the week
%x Local corresponding date representation
%X Local corresponding time representation
%Z Name of the current time zone
author :Leozhanggg
Source :https://www.cnblogs.com/leozhanggg/p/13954504.html
The copyright of this article belongs to the author and blog Park , Welcome to reprint , However, this statement must be retained without the consent of the author , And in the article page obvious position gives the original link , Otherwise, the right to pursue legal responsibility is reserved .