龙空技术网

Python笔记 日期时间 datetime 基础

蚊子不游戏 260

前言:

当前各位老铁们对“python通过周数得到日期”都比较看重,朋友们都需要分析一些“python通过周数得到日期”的相关文章。那么小编也在网上搜集了一些关于“python通过周数得到日期””的相关资讯,希望大家能喜欢,朋友们快快来了解一下吧!

日期时间基础,有兴趣的可以收了。

Python 日期时间

Python笔记 日期时间 datetime 基础

Python 支持六种日期时间对象,分别为:

tiemdeltadatedatetimetimetzinfotimezone

所有的对象都是不可变对象,日期时间嘛,当然不可变了,好理解。

1. timedelta 对象

timedelta 对象表示二个日期或时间之间的时间差

class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)

总共有7个可选参数,默认值都是0,参数可以是整数或者浮点数,正数或负数都行。

1.1 类属性

timedelta.min 最小的时间差 timedelta(-999999999)timedelta.max 最大的时间差 timedelta(days=999999999, hours=23, minutes=59, seconds=59, microseconds=999999)timedelta.resolution 最小时间间距 timedelta(microseconds=1) 1微秒

1.2 实例属性

属性值days-999999999 到 999999999seconds0 到 86399micrseconds0 到 999999

1.3 支持的操作

In [1]:

from datetime import timedeltat1 = timedelta(days = 3)t2 = timedelta(days = 1)

支持常见的+ - * / 等操作:

操作结果t3 = t1 + t2t1, t2求各后等于t3,t1 == t3 - t2, t2 == t3 -t1 返回Truet4 = t1 - t2t4 是t1 - t2 之差,t4 + t2 == t1, t4 = t1 - t2 返回Truet1 = t2 * i or t1 = i * t2t1 = t2 * i or t1 = i * t2f = t2 / t3返回一个浮点对象str(t)返回一个时间字符串,[D day[s], ][H]H:MM:SS[.UUUUUU]

其它更多操作参考Python官网。

In [2]:

t3 = t1 + t2t3

Out[2]:

datetime.timedelta(4)

In [3]:

t4 = t1 - t2t4t4 == t1 - t2

Out[3]:

True

In [4]:

t1 = t2 * 3t1

Out[4]:

datetime.timedelta(3)

In [5]:

f = t2 / t3f

Out[5]:

0.25

In [6]:

str(t1)

Out[6]:

'3 days, 0:00:00'

1.4 tiemdelta.total_seconds()

In [7]:

t1.total_seconds() # 返回折算成秒后的t1

Out[7]:

259200.0

2. date 对象

class datetime.date(year, month, day)

所有参数都必须且是整数,范围如下:

MINYEAR <= year <= MAXYEAR1 <= month <= 121 <= day <= number of days in the given month and year

2.1 类属性

date.min date(MINYEAR, 1, 1) 最小可表示的日期date.max date(MAXYEAR, 12, 31) 最大可表示的日期date.resolution timedelta(days=1) 最小间距 1天date.year 年date.month 月,1到12date.day 日,最小1,最大31

2.2 支持的操作

支持常见的+ - 比较 等操作:

操作结果date2 = date1 + timedeltadate1加一个时间差date2 = date1 - timedeltadate1减一个时间差timedelta = date1 - date2date - date 就是时间差date1 < date2比较

In [8]:

import datetimedate1 = datetime.date(2018,10,26)date2 = datetime.date(2018,10,27)n = timedelta(1)

In [9]:

date3 = date1 + ndate3

Out[9]:

datetime.date(2018, 10, 27)

In [10]:

date4 = date3 - ndate4

Out[10]:

datetime.date(2018, 10, 26)

In [11]:

date1 < date2

Out[11]:

True

2.3 类方法

date.today() 返回当地日期,与date.fromtimestamp(time.time())一样

In [12]:

datetime.date.today()

Out[12]:

datetime.date(2018, 10, 26)
date.fromtimestamp(timestamp) 根据时间戳返回当地时间,timestamp参数为必须

In [13]:

datetime.date.fromtimestamp(1540528412.7266233)

Out[13]:

datetime.date(2018, 10, 26)

2.4 实例方法

date.replace(year=self.year, month=self.month, day=self.day) 返回更改的日期

In [14]:

day1 = datetime.date(2018,10,26)day1.replace(day = 25)

Out[14]:

datetime.date(2018, 10, 25)
date.timetuple() 返回时期元组

In [15]:

day1.timetuple()

Out[15]:

time.struct_time(tm_year=2018, tm_mon=10, tm_mday=26, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=299, tm_isdst=-1)
date.toordinal() 返回日期的序数

In [16]:

day1.toordinal()

Out[16]:

736993
date.weekday() 返回周数

In [17]:

day1.weekday()

Out[17]:

4
date.isoweekday() 返回国标周数

In [18]:

day1.isoweekday()

Out[18]:

5
date.isocalendar() 返回包含三个值的元组(ISO year, ISO week number, ISO weekday)

In [19]:

day1.isocalendar()

Out[19]:

(2018, 43, 5)
date.isoformat() 返回国标日期格式字符串

In [20]:

day1.isoformat()

Out[20]:

'2018-10-26'
date.ctime() 返回ctime字符串

In [21]:

day1.ctime()

Out[21]:

'Fri Oct 26 00:00:00 2018'
date.strftime(format) 返回日期字符串

In [22]:

day1.strftime("%Y-%m-%d %H:%M:%S")

Out[22]:

'2018-10-26 00:00:00'

3. datetime 对象

datetime其实就是date和time的组合

class datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)

年月日是必要的参数,其它可选,每个参数的范围如下:

MINYEAR <= year <= MAXYEAR,

1 <= month <= 12,

1 <= day <= number of days in the given month and year,

0 <= hour < 24,

0 <= minute < 60,

0 <= second < 60,

0 <= microsecond < 1000000,

fold in [0, 1].

3.1 类方法

datetime.today() 返回当地日期

In [23]:

datetime.datetime.today()

Out[23]:

datetime.datetime(2018, 10, 26, 14, 2, 46, 442838)
datetime.now(tz=None) 返回当地日期,时区可选

In [24]:

datetime.datetime.now()

Out[24]:

datetime.datetime(2018, 10, 26, 14, 2, 46, 518842)
datetime.utcnow() UTC格式

In [25]:

datetime.datetime.utcnow()

Out[25]:

datetime.datetime(2018, 10, 26, 6, 2, 46, 585846)
datetime.fromtimestamp(timestamp, tz=None) 根据时间戳返回日期时间,时区可选

In [26]:

datetime.datetime.fromtimestamp(1540528412.7266233)

Out[26]:

datetime.datetime(2018, 10, 26, 12, 33, 32, 726623)
datetime.utcfromtimestamp(timestamp) 根据时间戳返回UTC时间

In [27]:

datetime.datetime.utcfromtimestamp(1540528412.7266233)

Out[27]:

datetime.datetime(2018, 10, 26, 4, 33, 32, 726623)
datetime.strptime(date_string, format) 日期字符串转datetime

In [28]:

datetime.datetime.strptime('2018-10-26',"%Y-%m-%d")

Out[28]:

datetime.datetime(2018, 10, 26, 0, 0)

3.2 类属性

datetime.min datetime(MINYEAR, 1, 1, tzinfo=Nonedatetime.max datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, tzinfo=None)datetime.resolution timedelta(microseconds=1)

3.3 实例方法

In [29]:

datetime1 = datetime.datetime(2018,10,26,10,26,33)datetime1 = datetime.datetime(2018,10,25,10,26,33)
datetime.date() 返回日期

In [30]:

datetime1.date()

Out[30]:

datetime.date(2018, 10, 25)
datetime.time() 返回时间

In [31]:

datetime1.time()

Out[31]:

datetime.time(10, 26, 33)
datetime.timetz() 返回时间及微秒等

In [32]:

datetime1.timetz()

Out[32]:

datetime.time(10, 26, 33)
datetime.replace(year=self.year, month=self.month, day=self.day, hour=self.hour, minute=self.minute, second=self.second, microsecond=self.microsecond, tzinfo=self.tzinfo, * fold=0) 更改时间

In [33]:

datetime1.replace(year=2017 )

Out[33]:

datetime.datetime(2017, 10, 25, 10, 26, 33)
datetime.weekday() 返回周数

In [34]:

datetime1.weekday()

Out[34]:

3

基本可以参照date及time

4. time对象

datetime.time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0) 所有参数可选

参数可选范围如下:

0 <= hour < 24,

0 <= minute < 60,

0 <= second < 60,

0 <= microsecond < 1000000,

fold in [0, 1]

In [35]:

datetime.time()

Out[35]:

datetime.time(0, 0)

4.1 类属性

time.min 最早的时间time(0, 0, 0, 0)time.max 最晚的时间time(23, 59, 59, 999999)time.resolution 时间间隔,最小timedelta(microseconds=1)

4.2 实例方法

time.replace(hour=self.hour, minute=self.minute, second=self.second, microsecond=self.microsecond, tzinfo=self.tzinfo, * fold=0) 更新时间

In [36]:

time1 = datetime.time(13,58,23)time1

Out[36]:

datetime.time(13, 58, 23)

In [37]:

time1.replace(20)

Out[37]:

datetime.time(20, 58, 23)
time.isoformat(timespec='auto') 国标格式

In [38]:

time1.isoformat()

Out[38]:

'13:58:23'
time.strftime(format) 时间格式化

In [39]:

time1.strftime('%H:%M:%S')

Out[39]:

'13:58:23'

标签: #python通过周数得到日期