python日期_Python日期

python日期

Python date class is part of datetime module.

Python日期类是datetime模块的一部分。

Python日期 (Python date)

Python date object represents a date (year, month and day) in a calendar. January 1 of year 1 is called day number 1, January 2 of year 1 is called day number 2, and so on.

Python日期对象代表日历中的日期(年,月和日)。 第1年的1月1日称为第1天,第1年的1月2日称为第2天,依此类推。

We can create date instance using the following factory method.

我们可以使用以下工厂方法创建日期实例。

datetime.date(year, month, day)

All the arguments are mandatory and should be an integer in the valid range.

所有参数都是强制性的,并且应为有效范围内的整数。

Year value should be in the range 1-9999, month should be in the range 1-12 and day should be in the range of valid days in the given month of the year.

年份值应在1-9999范围内,月份应在1-12范围内,日期应在一年中给定月份的有效天数范围内。

If the argument forms an invalid date, then ValueError will be raised.

如果参数形成无效的日期,则将引发ValueError

Python创建日期实例 (Python Create Date Instance)

We can create date instance from the factory method.

我们可以从工厂方法创建日期实例。

from datetime import date

d = date(2018, 12, 25)

print(d)

Output: 2018-12-25

产出: 2018-12-25

There are some class methods to create date instance too.

也有一些创建日期实例的类方法

创建今天的日期 (Create Today’s Date)

d = date.today()
print(d)

Output: 2018-09-18

输出: 2018-09-18

从时间戳创建日期 (Create Date from Timestamp)

import time
t = time.time()
print(t)
d = date.fromtimestamp(t)
print(d)
d = date.fromtimestamp(1537261418)
print(d)

Output:

输出:

1537265122.553337
2018-09-18
2018-09-18

从序号创建日期 (Create Date from Ordinal)

d = date.fromordinal(366)
print(d)

Output: 0002-01-01

输出: 0002-01-01

ISO字符串中的日期 (Date from ISO String)

A new method fromisoformat() has been added in Python 3.7 to create date instance from ISO format string. The input string should be in the format of YYYY-MM-DD.

Python 3.7中添加了一个新方法fromisoformat() ,以从ISO格式字符串创建日期实例。 输入字符串的格式应为YYYY-MM-DD

# date from ISO string format, added in Python 3.7
d = date.fromisoformat('2018-09-19')
print(d)

日期类别属性 (Date Class Attributes)

print(date.min)
print(date.max)
print(date.resolution)

Output:

输出:

0001-01-01
9999-12-31
1 day, 0:00:00

日期实例属性 (Date instance attributes)

Date instance attributes are read only.

日期实例属性是只读的。

d = date.today()
print(d.year)
print(d.month)
print(d.day)

Output:

输出:

2018
9
18

使用timedelta进行日期运算 (Date Operations with timedelta)

Date object supports arithmetic operators with timedelta instance to create future and past dates.

Date对象支持带有timedelta实例的算术运算符,以创建将来和过去的日期。

date_tomorrow = date.today() + timedelta(days=1)
print(date_tomorrow)
date_yesterday = date.today() - timedelta(days=1)
print(date_yesterday)

td = date_tomorrow - date_yesterday
print(td)

print(date_tomorrow > date_yesterday)

Output:

输出:

2018-09-19
2018-09-17
2 days, 0:00:00
True

日期实例方法 (Date instance methods)

Let’s look at some date instance methods.

让我们看一些日期实例方法。

replace(year = self.year,month = self.month,day = self.day) (replace(year=self.year, month=self.month, day=self.day))

Returns a date instance with same value, unless its new value is provided through keyword arguments.

返回具有相同值的日期实例,除非通过关键字参数提供其新值。

today = date.today()
print(today)
new_date = today.replace(year=2020)
print(new_date)

Output:

输出:

2018-09-18
2020-09-18

timetuple() (timetuple())

Return a time.struct_time instance, same as being returned by time.localtime().

返回一个time.struct_time实例,与time.localtime()返回的实例相同。

print(today.timetuple())

Output:

输出:

time.struct_time(tm_year=2018, tm_mon=9, tm_mday=18, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=261, tm_isdst=-1)

Notice that hour, minutes and seconds value will always be 0 and DST flag will always be -1.

请注意,小时,分钟和秒的值将始终为0,DST标志将始终为-1。

toordinal() (toordinal())

Returns the ordinal value of the date instance.

返回日期实例的序数值。

print(today.toordinal())

Output: 736955

输出: 736955

工作日() (weekday())

Return the day of the week as an integer, where Monday is 0 and Sunday is 6.

以整数形式返回星期几,其中星期一为0,星期日为6。

print(today.weekday()) # 2018-09-18 is Tuesday

Output: 1

输出1

isoweekday() (isoweekday())

Return the day of the week as an integer, where Monday is 1 and Sunday is 7.

以整数返回星期几,其中星期一为1,星期日为7。

print(today.isoweekday())

Output: 2

输出2

isocalendar() (isocalendar())

Returns a tuple (ISO year, ISO week number, ISO weekday).

返回一个元组(ISO年,ISO周号,ISO工作日)。

print(today.isocalendar())

Output: (2018, 38, 2)

产出: (2018, 38, 2)

isoformat() (isoformat())

Return a string representing the date in ISO 8601 format i.e. “YYYY-MM-DD”.

以ISO 8601格式返回表示日期的字符串,即“ YYYY-MM-DD”。

print(today.isoformat())

Output: 2018-09-18

输出: 2018-09-18

ctime() (ctime())

Returns a string representing the date instance.

返回表示日期实例的字符串。

print(today.ctime())

Output: Tue Sep 18 00:00:00 2018

输出: Tue Sep 18 00:00:00 2018

Python日期转换为格式化字符串 (Python Date to Formatted String)

We can use strftime() function to convert date instance to string with specified formatting.

我们可以使用strftime()函数将日期实例转换为具有指定格式的字符串。

print(today.strftime('%Y/%m/%d'))

Output: 2018/09/18

产出: 2018/09/18

Python将字符串转换为日期 (Python Convert String to Date)

We can use datetime strptime() function to convert string to datetime instance. Then we can use its date() function to convert to date instance.

我们可以使用datetime strptime()函数将字符串转换为datetime实例。 然后,我们可以使用其date()函数将其转换为日期实例。

from datetime import datetime
dt = datetime.strptime('2018/09/18', '%Y/%m/%d').date()
print(type(dt))
print(dt)

Output:

输出:

<class 'datetime.date'>
2018-09-18
GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/23338/python-date

python日期

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值