python日期和时间_Python-日期和时间

python日期和时间

python日期和时间

Python-日期和时间 (Python - Date and Time)

Often in data science we need analysis which is based on temporal values. Python can handle the various formats of date and time gracefully. The datetime library provides necessary methods and functions to handle the following scenarios.

在数据科学中,我们经常需要基于时间值的分析。 Python可以优雅地处理各种日期和时间格式。 日期时间库提供了处理以下情况的必要方法和功能。

  • Date Time Representation

    日期时间表示
  • Date Time Arithmetic

    日期时间算术
  • Date Time Comparison

    日期时间比较

We will study them one by one.

我们将一一研究。

日期时间表示 (Date Time Representation)

A date and its various parts are represented by using different datetime functions. Also, there are format specifiers which play a role in displaying the alphabetical parts of a date like name of the month or week day. The following code shows today's date and various parts of the date.

日期及其各个部分使用不同的datetime函数表示。 另外,有些格式说明符在显示日期的字母部分(例如月份或星期几的名称)中起作用。 以下代码显示了今天的日期和日期的各个部分。


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')

When we execute the above code, it produces the following result.

当我们执行上面的代码时,它产生以下结果。


The Date Today is  : 2018-04-22 15:38:35.835000
2018-04-22
This Year   : 2018
This Month    : 4
Month Name: April
This Week Day    : 22
Week Day Name: Sunday


日期时间算术 (Date Time Arithmetic)

For calculations involving dates we store the various dates into variables and apply the relevant mathematical operator to these variables.

对于涉及日期的计算,我们将各种日期存储到变量中,并将相关的数学运算符应用于这些变量。


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 

When we execute the above code, it produces the following result.

当我们执行上面的代码时,它产生以下结果。


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

日期时间比较 (Date Time Comparison)

Date and time are compared using logical operators. But we must be careful in comparing the right parts of the dates with each other. In the below examples we take the future and past dates and compare them using the python if clause along with logical operators.

使用逻辑运算符比较日期和时间。 但是我们在比较日期的正确部分时必须小心。 在以下示例中,我们获取了将来和过去的日期,并使用python if子句和逻辑运算符对它们进行了比较。


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'

When we execute the above code, it produces the following result.

当我们执行上面的代码时,它产生以下结果。


Today is:  2018-04-22
Before Four Days: 2018-04-18
date1: 2018-04-04
Past Date
Future Date




翻译自: https://www.tutorialspoint.com/python_data_science/python_date_and_time.htm

python日期和时间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值