Python date and time processing

datetimeIt is a built-in module of python, used to process date and time.

The commonly used classes of this module are:

Class name Function Description
date Date object
time Time object
datetime Datetime object
timedelta time interval
tzinfo Time zone information object

 

datetimeHow datetimeto use the classes in the module . datetimeDate and time the object is a combination of covers dateand timeall the information of the object.


One, import datetimeclass

The first step is to import the datetimeclass:

from datetime import datetime

 

Second, construct the datetimeobject

datetime(year,month,day,hour=0, minute=0, second=0, microsecond=0, tzinfo=None)

Parameter range:

  • MINYEAR <= year <= MAXYEAR

  • 1 <= month <= 12

  • 1 <= day <= Specified year and month celestial number

  • 0 <= hour < 24

  • 0 <= minute < 60

  • 0 <= second < 60

  • 0 <= microsecond < 1000000

Examples:

datetime(2019,12,6,13,30,50)
# 输出 : datetime.datetime(2019, 12, 6, 13, 30, 50)

Three, class method

  1. datetime.today()

Returns the current date and time datetimeobject in this region

datetime.today()
# 输出 : datetime.datetime(2019, 12, 9, 13, 27, 54, 693978)
  1. datetime.now(tz=None)

Returns the current date and time datetimeobject in the region , you can add a time zone tz When there is no time zone by default, the returned result is the datetime.today()same

datetime.now()
# 输出 : datetime.datetime(2019, 12, 9, 13, 27, 54, 693978)
  1. datetime.utcnow()

Returns UTC (same as Greenwich Mean Time) date and time datetimeobject

datetime.now()
# 输出 : datetime.datetime(2019, 12, 9, 13, 27, 54, 693978)
  1. datetime.fromtimestamp(timestamp, tz=None)

Returns the datetimeobject of the given timestamp , the time zone is empty by default, returns the local date and time, or you can specify the time zone

datetime.fromtimestamp(1575877756)
# 输出 : datetime.datetime(2019, 12, 9, 15, 49, 16)

If you want to get the current timestamp, you can use the timemodule timemethod:

import time
time.time()
# 输出:1575877756.4673727
  1. datetime.utcfromtimestamp(timestamp)

Returns the datetimeobject with a given timestamp , the date and time default to UTC (same as Greenwich Mean Time)

datetime.fromtimestamp(1575877756)
# 输出 : datetime.datetime(2019, 12, 9, 7, 49, 16)
  1. datetime.fromordinal(ordinal)

The input parameter is the number of days, and it returns the year, month, and day datetimeobject counted from 0 AD. For example, if the input parameter is 365, it will return 1-12-31

datetime.fromordinal(365)
# 输出 : datetime.datetime(1, 12, 31, 0, 0)
  1. datetime.combine(date, time, tzinfo=self.tzinfo)

Splice dateand timeobject to form a new datetimeobject. You can enter the time zone parameter, otherwise it will default to the original timetime zone

date_ = datetime.today().date()
time_ = datetime.today().time()
datetime.combine(date_,time_)
# 输出 : datetime.datetime(2019, 12, 9, 16, 12, 56, 914484)
  1. datetime.strptime(date_string, format)

Convert the formatted date and time string into an datetimeobject, you can convert date, time, date and time

datetime.strptime('2019-11-05','%Y-%m-%d')
# 输出 : datetime.datetime(2019, 11, 5, 0, 0)
datetime.strptime('09:30:50','%H:%M:%S')
# 输出 : datetime.datetime(1900, 1, 1, 9, 30, 50)
datetime.strptime('2019-11-05 09:30:50','%Y-%m-%d %H:%M:%S')
# 输出 : datetime.datetime(2019, 11, 5, 9, 30, 50)

Four, instance method

  1. datetime.date()

Return dateobject

d = datetime(2019,12,6,13,30,50)
d.date()
# 输出 : datetime.date(2019, 12, 6)
  1. datetime.time()

Return timeobject

d = datetime(2019,12,6,13,30,50)
d.time()
# 输出 : datetime.time(13, 30, 50)
  1. datetime.timestamp()

datetimeReturn a timestamp for the given  object

d = datetime(2019,12,6,13,30,50)
d.timestamp()
# 输出 : 1575610250.0
  1. datetime.weekday()

Returns the day of the week, Monday is 0, Sunday is 6

d = datetime(2019,12,6,13,30,50)
d.weekday()
# 输出 : 4
  1. datetime.isoweekday()

Returns the day of the week, Monday is 1 and Sunday is 7

d = datetime(2019,12,6,13,30,50)
d.isoweekday()
# 输出 : 5
  1. datetime.isocalendar()

Return array: (year, week number, day of week)

d = datetime(2019,12,6,13,30,50)
d.isocalendar()
# 输出 : (2019, 49, 5)
  1. datetime.ctime()

Returns the string representation of the date and time

d = datetime(2019,12,6,13,30,50)
d.ctime()
# 输出 : 'Fri Dec  6 13:30:50 2019'
  1. datetime.strftime(date_string, format)

Convert datetimeobject to format string

d = datetime.today()
datetime.strftime(d,'%Y-%m-%d %H:%M:%S')
# 输出 : '2019-12-09 16:32:18'
  1. datetime.toordinal()

Returns the datetimenumber of days from 0 AD to the specified

d = datetime(2019,12,6,13,30,50)
d.toordinal()
# 输出 : 737399

Five, class instance attributes

  1. datetime.year

Returns datetimethe year of the given object

d = datetime(2019,12,6,13,30,50)
d.year
# 输出 : 2019
  1. datetime.month

Returns datetimethe month of the given object

d = datetime(2019,12,6,13,30,50)
d.month
# 输出 : 12
  1. datetime.day

Returns the datetimenumber of days of the given object (day of the month)

d = datetime(2019,12,6,13,30,50)
d.day
# 输出 : 6
  1. datetime.hour

Returns the datetimenumber of hours (time of day) for the given object

d = datetime(2019,12,6,13,30,50)
d.hour
# 输出 : 13
  1. datetime.minute

Returns the datetimenumber of minutes (a minute of an hour) for a given object

d = datetime(2019,12,6,13,30,50)
d.minute
# 输出 : 30
  1. datetime.second

Returns datetimethe number of seconds of the given object (a second of a minute)

d = datetime(2019,12,6,13,30,50)
d.second
# 输出 : 50
  1. datetime.microsecond

Returns the datetimenumber of milliseconds for the given object (a second in a minute)

d = datetime(2019,12,6,13,30,50,3000)
d.microsecond
# 输出 : 3000
  1. datetime.tzinfo

Returns datetimethe time zone of the object, provided that the parameters datetimeneed to be passed in when creating the object tzinfo, if not passed in, the return value is None.

# 获取一个含有时区的datetime对象
import pytz
sh = pytz.timezone('Asia/Shanghai')
d = datetime(2018, 11, 1, hour=8, tzinfo=sh)
d.tzinfo
# 输出 : <DstTzInfo 'Asia/Shanghai' LMT+8:06:00 STD>

6. Conclusion

datetimedatetimeVarious methods and attributes of the classes in the module, rich code cases, divided into categories, easy to find when using.

 

Guess you like

Origin blog.csdn.net/ytp552200ytp/article/details/108203323