和小姐姐撸Python

Python datetime模块

2017-12-13  本文已影响0人  Modelstrategy
# datetime date 模块

from datetime import date

christmas = date(2017, 12, 25)
print(type(christmas))
print(christmas)
print(christmas.year)
print(christmas.month)
print(christmas.day)
print(christmas.isoformat())
print('min ', date.min, 'max ', date.max)

# 使用today方法声称当前日期

now = date.today()
print(now)

# 使用timedelta实现date的加法

from datetime import timedelta
one_day = timedelta( days = 1)
tomorrow = now + one_day
print(tomorrow)

yesterday = now - one_day
print(yesterday)
print(christmas - now)

# datetime time 模块

from datetime import time
noon = time(12, 0 ,0)
print(noon)
print(noon.hour)
print(noon.minute)
print(noon.second)
print(noon.microsecond)

# datetime 模块

from datetime import datetime
some_day = datetime(2017, 12, 13, 16, 3, 3)
print('untouched', some_day)
print('isoformat', some_day.isoformat())

# datetime 模块的 now 方法

now = datetime.now()
print('now', now)
print('month', now.month)
print('day', now.day)
print('minute', now.minute)
print('second', now.second)
print('microsecond', now.microsecond)

# 用combine方法将date对象和一个time对象合并为一个datetime对象

from datetime import datetime, time, date
noon = time(12)
this_day = date.today()
noon_today = datetime.combine(this_day, noon)
print('combined datetime', noon_today.isoformat())

# 用datetime重做  输入某年某月某日,判断这一天是这一年的第几天

a_list = []
for i in range(3):
  a_list.append(int(input('please, enter the year first, I beg you')))

anfang_des_jahres = datetime(a_list[0], 1, 1)
date_entered = datetime(*a_list)

print('days:', date_entered - anfang_des_jahres)

上一篇下一篇

猜你喜欢

热点阅读