📜  在Python中处理日期

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

在Python中处理日期

在本文中,我们将讨论如何使用Python处理日期。

Python使处理日期和时间变得非常容易,我们所要做的就是导入一个名为 DateTime 的模块,该模块与Python一起提供。这是一种更有效的处理日期的方法,无需明确编程。通过使用此模块,您将从本地计算机所在的时区获取时间和日期。该模块只是从主机(在这种情况下,它是执行程序的计算机)获取日期和时间。

日期类

在深入研究函数之前。让我们从一个简单的程序开始,该程序使用 datetime 模块中的日期对象返回今天的日期。



句法:

示例:获取当前日期

Python3
# importing the module
from datetime import date
  
# storing today's date in a variable
today = date.today()
  
# Printing the variable
print(f"Today's date: {today}")


Python3
from datetime import date
  
today = date.today()
  
# to print the present year
print(f"Present Year:{today.year}")
  
# to print the present month
print(f"Present Month:{today.month}")
  
# to print the present date
print(f"Present Date:{today.day}")


Python3
# Importing date from Datetime module
from datetime import date
  
# Storing today's date into a variable
today = date.today()
  
# Storing the specific date
graduation_day = date(2023, 8, 11)
  
# finding the difference of today's date from 
# specified date
days_left = abs(graduation_day - today)
  
# Displaying the no.of.days left without time
print(f"Number of days left till graduation: {days_left}")


Python3
from datetime import datetime
  
# By using now(), We will get both current 
# date and time Storing current time and
# date into a variable
today = datetime.now()
  
# Storing the date and time you want to calculate
# In this you have to give the time as the input
graduation_day = datetime(2023, 8, 11, 0, 0, 0)
  
# finding the difference from
days_left = abs(graduation_day - today)
  
# Displaying the no.of.days left with time
print(f"Time left till the graduation: {days_left}")


Python3
from datetime import datetime, timedelta
  
# adding 9 hours
hour_delta = timedelta(hours=9)
  
# Storing the new date
timeAdded = datetime.now() + hour_delta
  
# Displaying current date and time with 
# adding 9 hours
print(f"The date after adding 9 hours: {timeAdded}")
  
# adding 1 day
day_delta = timedelta(days=1)
  
# storing the new date
dateAdded = datetime.now() + day_delta
  
# Displaying current date and time with 
# adding 1 day
print(f"The date after adding 1 day: {dateAdded}")


Python3
import datetime
  
today = datetime.datetime(2021, 9, 10, 12, 30, 30)
  
print(today.strftime("%H:%M:%S %B %d %Y"))


Python3
from datetime import datetime
  
print(datetime.strptime('26/5/2020', '%d/%m/%Y'))


输出:

today()方法将为您提供时区的当前日期。如果您只需要年、月或日,则可以将此方法与相应的关键字一起使用。

示例:明智地获取日期属性

蟒蛇3

from datetime import date
  
today = date.today()
  
# to print the present year
print(f"Present Year:{today.year}")
  
# to print the present month
print(f"Present Month:{today.month}")
  
# to print the present date
print(f"Present Date:{today.day}")

输出:



Present Year:2021
Present Month:9
Present Date:28

示例:计算从当前日期到特定日期的天数的程序

蟒蛇3

# Importing date from Datetime module
from datetime import date
  
# Storing today's date into a variable
today = date.today()
  
# Storing the specific date
graduation_day = date(2023, 8, 11)
  
# finding the difference of today's date from 
# specified date
days_left = abs(graduation_day - today)
  
# Displaying the no.of.days left without time
print(f"Number of days left till graduation: {days_left}")

输出:

Number of days left till graduation: 682 days, 0:00:00

日期时间类

此方法类似于日期方法,但这是日期对象的升级版本,它将从本地系统中获取日期和时间。让我们编写与日期对象相同的程序,但这次是计算剩余天数。

示例:计算剩余天数的程序

蟒蛇3

from datetime import datetime
  
# By using now(), We will get both current 
# date and time Storing current time and
# date into a variable
today = datetime.now()
  
# Storing the date and time you want to calculate
# In this you have to give the time as the input
graduation_day = datetime(2023, 8, 11, 0, 0, 0)
  
# finding the difference from
days_left = abs(graduation_day - today)
  
# Displaying the no.of.days left with time
print(f"Time left till the graduation: {days_left}")

输出:

时间增量类

此对象允许我们在特定日期和时间上添加或减去时间或日期。

句法 :

示例:加减时间



蟒蛇3

from datetime import datetime, timedelta
  
# adding 9 hours
hour_delta = timedelta(hours=9)
  
# Storing the new date
timeAdded = datetime.now() + hour_delta
  
# Displaying current date and time with 
# adding 9 hours
print(f"The date after adding 9 hours: {timeAdded}")
  
# adding 1 day
day_delta = timedelta(days=1)
  
# storing the new date
dateAdded = datetime.now() + day_delta
  
# Displaying current date and time with 
# adding 1 day
print(f"The date after adding 1 day: {dateAdded}")

输出:

解析和格式化

如果我们想将日期转换为可读字符串,则使用strftime()方法。

句法:

time.strftime(format, t)

参数:

  • 格式 -这是字符串类型。即指令可以嵌入格式字符串中。
  • t –要格式化的时间。

示例:将日期转换为字符串

蟒蛇3

import datetime
  
today = datetime.datetime(2021, 9, 10, 12, 30, 30)
  
print(today.strftime("%H:%M:%S %B %d %Y"))

输出:

12:30:30 September 10 2021

解析用于将字符串转换为日期时间格式。 strptime()用于执行此操作。参数分别是 date_string 和 format。

句法 :

示例:将日期时间转换为字符串

蟒蛇3

from datetime import datetime
  
print(datetime.strptime('26/5/2020', '%d/%m/%Y'))

输出:

2020-05-26 00:00:00