返回列表 发帖

datetime模块

例子一:(显示当前的日期)
#coding=utf-8
import datetime

a = datetime.datetime.today()
print(a)
print(type(a))

print("-" * 10)
b = str(a) #这里不先转换为字符串,下面使用split()时就会报错

c = b.split()[0]
print(c)
print(type(c))

print("-" * 10)
cc = datetime.datetime.strptime(c,'%Y-%m-%d').date() #字符串转换为日期
print(cc)
print(type(cc))


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
2019-10-08 18:40:38.867039
<class 'datetime.datetime'>
----------
2019-10-08
<class 'str'>
----------
2019-10-08
<class 'datetime.date'>

C:\Users\jacky\Desktop>



######
例子二:(显示当前的日期时间)
#coding=utf-8
import datetime

print(datetime.datetime.now())

a = datetime.datetime.now()
print(a)
print(type(a))

print("-" * 10)
print("-" * 10)

b = str(a) #这里不先转换为字符串,下面使用split()时就会报错

c = b.split()[0]
print(c)
print(type(c))

print("-" * 10)
c1 = c.split("-")[0]
print(c1)
print(type(c1))

c2 = c.split("-")[1]
print(c2)
print(type(c2))

c3 = c.split("-")[2]
print(c3)
print(type(c3))

c4 = c.split("-")[2]
c4 = int(c4) #字符串转换为整数
print(c4)
print(type(c4))

print("-" * 10)
print("-" * 10)
d = b.split()[1].split(".")[0]
print(d)
print(type(d))

print("-" * 10)
d1 = d.split(":")[0]
print(d1)
print(type(d1))

d2 = d.split(":")[1]
print(d2)
print(type(d2))

d3 = d.split(":")[2]
print(d3)
print(type(d3))

dd = datetime.datetime.strptime(d,'%H:%M:%S').time() #字符串转换为时间
print(dd)
print(type(dd))


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
2019-10-08 18:46:16.920374
2019-10-08 18:46:16.925374
<class 'datetime.datetime'>
----------
----------
2019-10-08
<class 'str'>
----------
2019
<class 'str'>
10
<class 'str'>
08
<class 'str'>
8
<class 'int'>
----------
----------
18:46:16
<class 'str'>
----------
18
<class 'str'>
46
<class 'str'>
16
<class 'str'>
18:46:16
<class 'datetime.time'>

C:\Users\jacky\Desktop>



######
例子三:(显示当前的日期时间信息)
#coding=utf-8
import datetime

dt = datetime.datetime.now()
print("当前具体日期时间:",dt)

print("-" * 10)
print(f"日期时间%datetime: {dt.strftime('%c')}")
print(f"日期#date: {dt.strftime('%x')}")

Key_1 = dt.strftime('%x').split("/")[2]
print(Key_1)
Key_1 = dt.strftime('%x').split("/")[0]
print(Key_1)
Key_1 = dt.strftime('%x').split("/")[1]
print(Key_1)

print(f"时间$time: {dt.strftime('%X')}")
Key_2 = dt.strftime('%X').split(":")[0]
print(Key_2)
Key_2 = dt.strftime('%X').split(":")[1]
print(Key_2)
Key_2 = dt.strftime('%X').split(":")[2]
print(Key_2)

print("-" * 10)
tt = dt.strftime('%Y-%m-%d %H:%M:%S')
print(f"自定义显示格式(%Y-%m-%d %H:%M:%S): {tt}")

ttt = tt.split()[0]
print(f"自定义显示格式(%Y-%m-%d): {ttt}")
print(f"自定义显示格式(%Y-%m-%d %H:%M:%S %p): {dt.strftime('%y-%m-%d %I:%M:%S %p')}")

print("-" * 10)
print("-" * 10)
print(f"简写星期几: {dt.strftime('%a')}")
print(f"详写星期几: {dt.strftime('%A')}")
print(f"简写月份: {dt.strftime('%b')}")
print(f"详写月份: {dt.strftime('%B')}")

print("-" * 10)
print("-" * 10)
print(f"今天是一周中的第{dt.strftime('%w')}天")
print(f"今天是今年的第{dt.strftime('%j')}天")
print(f"这周是今年的第{dt.strftime('%U')}周")


脚本运行的结果:(星期日为一周中的第0天)
C:\Users\jacky\Desktop>python xx.py
当前具体日期时间: 2019-10-08 22:12:33.898297
----------
日期时间%datetime: Tue Oct  8 22:12:33 2019
日期#date: 10/08/19
19
10
08
时间$time: 22:12:33
22
12
33
----------
自定义显示格式(%Y-%m-%d %H:%M:%S): 2019-10-08 22:12:33
自定义显示格式(%Y-%m-%d): 2019-10-08
自定义显示格式(%Y-%m-%d %H:%M:%S %p): 19-10-08 10:12:33 PM
----------
----------
简写星期几: Tue
详写星期几: Tuesday
简写月份: Oct
详写月份: October
----------
----------
今天是一周中的第2天
今天是今年的第281天
这周是今年的第40周

C:\Users\jacky\Desktop>



######
例子四:(自定义当前日期时间的显示格式)
#coding=utf-8
import datetime

dt = datetime.datetime.now()
print(f"当前具体日期时间:{dt}")

a = dt.strftime('%Y-%m-%d')
print(a)

b = dt.strftime('%Y%m%d')
print(b)

c = dt.strftime('%Y%m%d-%H%M%S')
print(c)

tt = dt.strftime('%Y%m%d_%H%M%S')
print(tt)


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
当前具体日期时间:2019-10-08 19:23:10.670993
2019-10-08
20191008
20191008-192310
20191008_192310

C:\Users\jacky\Desktop>





######
例子五:(显示昨天的日期)
#coding=utf-8
import datetime
def func1():
    day1 = datetime.date.today()
    day2 = datetime.timedelta(days=1)
    Result_1 = day1 - day2  
    return Result_1

if __name__ == '__main__':
        Result_1 = func1()
        print(Result_1)


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
2019-10-07

C:\Users\jacky\Desktop>



######
例子六:(显示前天的日期)
#coding=utf-8
import datetime
def func1():
    day1 = datetime.date.today()
    day2 = datetime.timedelta(days=2)
    Result_1 = day1 - day2  
    return Result_1

if __name__ == '__main__':
        Result_1 = func1()
        print(Result_1)


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
2019-10-06

C:\Users\jacky\Desktop>



######
例子七:(显示三天前的日期)
#coding=utf-8
import datetime
def func1():
    day1 = datetime.date.today()
    day2 = datetime.timedelta(days=3)
    Result_1 = day1 - day2  
    return Result_1

if __name__ == '__main__':
        Result_1 = func1()
        print(Result_1)


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
2019-10-05

C:\Users\jacky\Desktop>



######
例子八:(显示明天的日期)
#coding=utf-8
import datetime
def func1():
    day1 = datetime.date.today()
    day2 = datetime.timedelta(days=1)
    Result_1 = day1 + day2  
    return Result_1

if __name__ == '__main__':
        Result_1 = func1()
        print(Result_1)


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
2019-10-09

C:\Users\jacky\Desktop>



######
例子九:(显示后天的日期)
#coding=utf-8
import datetime
def func1():
    day1 = datetime.date.today()
    day2 = datetime.timedelta(days=2)
    Result_1 = day1 + day2  
    return Result_1

if __name__ == '__main__':
        Result_1 = func1()
        print(Result_1)


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
2019-10-10

C:\Users\jacky\Desktop>



######
例子十:(显示三天后的日期)
#coding=utf-8
import datetime
def func1():
    day1 = datetime.date.today()
    day2 = datetime.timedelta(days=3)
    Result_1 = day1 + day2  
    return Result_1

if __name__ == '__main__':
        Result_1 = func1()
        print(Result_1)
        print(type(Result_1))


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
2019-10-11
<class 'datetime.date'>

C:\Users\jacky\Desktop>





######
例子十一:(计算两个日期之间相隔多少天)
#coding=utf-8
import datetime

date1 = '2019-9-13'
date2 = '2019-09-18'

a = datetime.datetime.strptime(date1, "%Y-%m-%d").date()
b = datetime.datetime.strptime(date2, "%Y-%m-%d").date()
c = b - a

print(a)
print(b)
print(c)
print(type(c))

print("-" * 10)

d = str(c).split()[0] #这里不先转换为字符,使用split()时就会报错
print(d)
print(type(d))

d = int(d) #字符串转换为整数
print(d)
print(type(d))


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
2019-09-13
2019-09-18
5 days, 0:00:00
<class 'datetime.timedelta'>
----------
5
<class 'str'>
5
<class 'int'>

C:\Users\jacky\Desktop>



######
例子十二:(计算两个日期之间相隔多少天)
#coding=utf-8
import datetime

date1 = '2019-9-13'
date2 = '2019-10-18'

a = datetime.datetime.strptime(date1, "%Y-%m-%d").date()
b = datetime.datetime.strptime(date2, "%Y-%m-%d").date()
c = b - a

print(a)
print(b)
print(c)
print(type(c))

print("-" * 10)

d = str(c).split()[0] #这里不先转换为字符,使用split()时就会报错
print(d)
print(type(d))

d = float(d) #字符串转换为浮点数
print(d)
print(type(d))


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
2019-09-13
2019-10-18
35 days, 0:00:00
<class 'datetime.timedelta'>
----------
35
<class 'str'>
35.0
<class 'float'>

C:\Users\jacky\Desktop>





######
例子十三:(计算两个时间之间相隔多少秒钟)
#coding=utf-8
import datetime

date1 = '2019-9-13 17:55:50'
date2 = '2019-9-18 17:56:56'

a = datetime.datetime.strptime(date1, "%Y-%m-%d %H:%M:%S")
b = datetime.datetime.strptime(date2, "%Y-%m-%d %H:%M:%S")
Result_1 = (b - a).seconds
#注释:seconds获得的秒钟数只是小时、分钟、秒钟的时间差总和,并没有计算天数等

print(a)
print(b)
print(Result_1)
print(type(Result_1))

time1 = date1.split()[1]
time2 = date2.split()[1]

print(f"'{time1}'和'{time2}'之间相差{Result_1}秒钟")


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
2019-09-13 17:55:50
2019-09-18 17:56:56
66
<class 'int'>
'17:55:50'和'17:56:56'之间相差66秒钟

C:\Users\jacky\Desktop>



######
例子十四:(计算两个时间之间相隔多少秒钟)
#coding=utf-8
import datetime

date1 = '17:55:50'
date2 = '17:56:56'

a = datetime.datetime.strptime(date1, "%H:%M:%S")
b = datetime.datetime.strptime(date2, "%H:%M:%S")
Result_1 = (b - a).seconds

print(a)
print(b)
print(Result_1)
print(type(Result_1))

print(f"'{date1}'和'{date2}'之间相差{Result_1}秒钟")


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
1900-01-01 17:55:50
1900-01-01 17:56:56
66
<class 'int'>
'17:55:50'和'17:56:56'之间相差66秒钟

C:\Users\jacky\Desktop>



######
例子十五:(计算两个时间之间相隔多少分钟)
#coding=utf-8
import datetime

date1 = '2019-9-13 17:55:50'
date2 = '2019-9-18 17:56:56'

a = datetime.datetime.strptime(date1, "%Y-%m-%d %H:%M:%S")
b = datetime.datetime.strptime(date2, "%Y-%m-%d %H:%M:%S")
Result_1 = (b - a).seconds

Result_2 = Result_1 / 60 #秒钟数转换为分钟数
Result_2 = int(Result_2) #只取整数部分,不是四舍五入

print(a)
print(b)
print(Result_2)
print(type(Result_2))

time1 = date1.split()[1]
time2 = date2.split()[1]

print(f"'{time1}'和'{time2}'之间相差{Result_2}分钟")


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
2019-09-13 17:55:50
2019-09-18 17:56:56
1
<class 'int'>
'17:55:50'和'17:56:56'之间相差1分钟

C:\Users\jacky\Desktop>



######
例子十六:(计算两个时间之间相隔多少分钟)
#coding=utf-8
import datetime

date1 = '17:55:50'
date2 = '17:56:56'

a = datetime.datetime.strptime(date1, "%H:%M:%S")
b = datetime.datetime.strptime(date2, "%H:%M:%S")
Result_1 = (b - a).seconds

Result_2 = Result_1 / 60 #秒钟数转换为分钟数
Result_2 = int(Result_2) #只取整数部分,不是四舍五入

print(a)
print(b)
print(Result_2)
print(type(Result_2))

print(f"'{date1}'和'{date2}'之间相差{Result_2}分钟")


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
1900-01-01 17:55:50
1900-01-01 17:56:56
1
<class 'int'>
'17:55:50'和'17:56:56'之间相差1分钟

C:\Users\jacky\Desktop>



######
例子十七:(计算两个时间之间相隔多少分钟)
#coding=utf-8
import datetime

date1 = '17:55:50'
date2 = '18:56:56'

a = datetime.datetime.strptime(date1, "%H:%M:%S")
b = datetime.datetime.strptime(date2, "%H:%M:%S")
Result_1 = (b - a).seconds

Result_2 = Result_1 / 60 #秒钟数转换为分钟数
Result_2 = int(Result_2) #只取整数部分,不是四舍五入

print(a)
print(b)
print(Result_2)
print(type(Result_2))

print(f"'{date1}'和'{date2}'之间相差{Result_2}分钟")


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
1900-01-01 17:55:50
1900-01-01 18:56:56
61
<class 'int'>
'17:55:50'和'18:56:56'之间相差61分钟

C:\Users\jacky\Desktop>



######
例子十八:(计算两个时间之间相隔多少小时)
#coding=utf-8
import datetime

date1 = '2019-9-13 7:55:50'
date2 = '2019-9-18 17:56:56'

a = datetime.datetime.strptime(date1, "%Y-%m-%d %H:%M:%S")
b = datetime.datetime.strptime(date2, "%Y-%m-%d %H:%M:%S")
Result_1 = (b - a).seconds

Result_2 = Result_1 / 60 / 60 #秒钟数转换为小时数
Result_2 = int(Result_2) #只取整数部分,不是四舍五入

print(a)
print(b)
print(Result_2)
print(type(Result_2))

time1 = date1.split()[1]
time2 = date2.split()[1]

print(f"'{time1}'和'{time2}'之间相差{Result_2}小时")


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
2019-09-13 07:55:50
2019-09-18 17:56:56
10
<class 'int'>
'7:55:50'和'17:56:56'之间相差10小时

C:\Users\jacky\Desktop>



######
例子十九:(计算两个时间之间相隔多少小时)
#coding=utf-8
import datetime

date1 = '2019-9-13 7:55:50'
date2 = '2019-9-18 17:3:56'

a = datetime.datetime.strptime(date1, "%Y-%m-%d %H:%M:%S")
b = datetime.datetime.strptime(date2, "%Y-%m-%d %H:%M:%S")
Result_1 = (b - a).seconds

Result_2 = Result_1 / 60 / 60 #秒钟数转换为小时数
Result_2 = int(Result_2) #只取整数部分,不是四舍五入

print(a)
print(b)
print(Result_2)
print(type(Result_2))

time1 = date1.split()[1]
time2 = date2.split()[1]

print(f"'{time1}'和'{time2}'之间相差{Result_2}小时")


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
2019-09-13 07:55:50
2019-09-18 17:03:56
9
<class 'int'>
'7:55:50'和'17:3:56'之间相差9小时

C:\Users\jacky\Desktop>



######
例子二十:(计算两个时间之间相隔多少小时)
#coding=utf-8
import datetime

date1 = '7:55:50'
date2 = '17:3:56'

a = datetime.datetime.strptime(date1, "%H:%M:%S")
b = datetime.datetime.strptime(date2, "%H:%M:%S")
Result_1 = (b - a).seconds

Result_2 = Result_1 / 60 / 60 #秒钟数转换为小时数
Result_2 = int(Result_2) #只取整数部分,不是四舍五入

print(a)
print(b)
print(Result_2)
print(type(Result_2))

print(f"'{date1}'和'{date2}'之间相差{Result_2}小时")

print("-" * 10)
time1 = datetime.datetime.strptime(date1,'%H:%M:%S').time() #字符串转换为时间
time2 = datetime.datetime.strptime(date2,'%H:%M:%S').time()

print(f"'{time1}'和'{time2}'之间相差{Result_2}小时")


脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
1900-01-01 07:55:50
1900-01-01 17:03:56
9
<class 'int'>
'7:55:50'和'17:3:56'之间相差9小时
----------
'07:55:50'和'17:03:56'之间相差9小时

C:\Users\jacky\Desktop>





相关文章:
time模块
自定义函数

split()函数
dateutil.rrule()函数

Windows使用tcping+任务计划
Python3脚本管理MSSQL2014

返回列表