Python数据处理
Python数据可视化
Python统计分析

Python日期和时间

Python日期和时间详细操作教程
通常在数据科学中,我们需要基于时间值的分析。 Python可以优雅地处理各种格式的日期和时间。 日期时间库提供了必要的方法和函数来处理下列情况。
日期时间表示 日期时间算术 日期时间比较
接下来,我们将会逐个学习。

日期时间表示

日期及其各个部分用不同的日期时间函数表示。 此外,还有格式说明符,它们在显示日期的字母部分(如月份或星期几的名称)中发挥作用。 以下代码显示了今天的日期和日期的各个部分。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-22
import datetime
print 'The Date Today is :', datetime.datetime.today()
date_today = datetime.date.today()
print (date_today)
print ('This Year :', date_today.year)
print ('This Month :', date_today.month(
print ('Month Name:',date_today.strftime('%B'))
print ('This Week Day :', date_today.day))
print ('Week Day Name:',date_today.strftime('%A'))
执行上面示例代码,得到以下结果 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-22
The Date Today is : 2018-05-22 15:38:35.835000
2018-05-22
This Year : 2018
This Month : 4
Month Name: May
This Week Day : 22
Week Day Name: Sunday

日期时间运算

对于涉及日期的计算,我们将各种日期存储到变量中,并将相关的数学运算符应用于这些变量。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-22
import datetime
#Capture the First Date
day1 = datetime.date(2018, 2, 12)
print ('day1:', day1.ctime())
# Capture the Second Date
day2 = datetime.date(2017, 8, 18)
print ('day2:', day2.ctime())
# Find the difference between the dates
print ('Number of Days:', day1-day2)
date_today = datetime.date.today()
# Create a delta of Four Days
no_of_days = datetime.timedelta(days=4)
# Use Delta for Past Date
before_four_days = date_today - no_of_days
print ('Before Four Days:', before_four_days )
# Use Delta for future Date
after_four_days = date_today + no_of_days
print 'After Four Days:', after_four_days
执行上面示例代码,得到以下结果 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-22
day1: Mon Feb 12 00:00:00 2018
day2: Fri Aug 18 00:00:00 2017
Number of Days: 178 days, 0:00:00
Before Four Days: 2018-04-18
After Four Days: 2018-04-26

日期时间比较

日期和时间使用逻辑运算符进行比较。必须小心将日期的正确部分进行比较。 在下面的例子中,我们采用未来和过去的日期,并使用python if子句和逻辑运算符进行比较。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-22
import datetime
date_today = datetime.date.today()
print ('Today is: ', date_today)
# Create a delta of Four Days
no_of_days = datetime.timedelta(days=4)
# Use Delta for Past Date
before_four_days = date_today - no_of_days
print ('Before Four Days:', before_four_days )
after_four_days = date_today + no_of_days
date1 = datetime.date(2018,4,4)
print ('date1:',date1)
if date1 == before_four_days :
    print ('Same Dates')
if date_today > date1:
    print ('Past Date')
if date1 < after_four_days:
    print ('Future Date')
执行上面示例代码,得到以下结果 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-22
Today is: 2018-04-22
Before Four Days: 2018-04-18
date1: 2018-04-04
Past Date
Future Date
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4