📜  Python中的时间函数 | Set-2(日期操作)

📅  最后修改于: 2022-05-13 01:55:07.740000             🧑  作者: Mango

Python中的时间函数 | Set-2(日期操作)

第 1 组中讨论了一些时间函数

日期操作也可以使用Python使用“datetime”模块并在其中使用“date”类来执行。

日期操作:

1. MINYEAR :- 它显示可以使用日期类表示的最小年份

2. MAXYEAR :- 它显示可以使用日期类表示的最大年份

# Python code to demonstrate the working of
# MINYEAR and MAXYEAR
  
# importing built in module datetime
import datetime
from datetime import date
  
# using MINYEAR to print minimum representable year
print ("Minimum representable year is : ",end="")
print (datetime.MINYEAR)
  
# using MAXYEAR to print maximum representable year
print ("Maximum representable year is : ",end="")
print (datetime.MAXYEAR)

输出:

Minimum representable year is : 1
Maximum representable year is : 9999

3. date(yyyy-mm-dd) :- 该函数返回一个字符串,其中传入的参数按年、月和日期的顺序排列。

4. today() :- 以 yyyy-mm-dd 格式返回当前日期

# Python code to demonstrate the working of
# date() and today()
  
# importing built in module datetime
import datetime
from datetime import date
  
# using date() to represent date
print ("The represented date is : ",end="")
print (datetime.date(1997,4,1))
  
# using today() to print present date
print ("Present date is : ",end="")
print (date.today())

输出:

The represented date is : 1997-04-01
Present date is : 2016-08-02

5. fromtimestamp(sec) :- 它返回从参数中提到的纪元以来经过的秒数计算的日期

6. min() :- 返回可以由日期类表示的最小日期

7. max() :- 返回日期类可以表示的最大日期

# Python code to demonstrate the working of
# fromtimestamp(), min() and max()
  
# importing built in module datetime
import datetime
from datetime import date
  
# using fromtimestamp() to calculate date
print ("The calculated date from seconds is : ",end="")
print (date.fromtimestamp(3452435))
  
# using min() to print minimum representable date
print ("Minimum representable date is : ",end="")
print (date.min)
  
# using max() to print minimum representable date
print ("Maximum representable date is : ",end="")
print (date.max)

输出:

The calculated date from seconds is : 1970-02-09
Minimum representable date is : 0001-01-01
Maximum representable date is : 9999-12-31