Python - time conversion

巴扎黑
Release: 2016-12-03 10:25:05
Original
1465 people have browsed it

Use the datetime module to convert different time units

from datetime import timedelta   
a = timedelta(days=2, hours=6)  
b = timedelta(hours=4.5)  
c = a + b  
print(c.days)  
print(c.seconds)  
print(c.seconds / 3600)
Copy after login

Represent a specific date and time

from datetime import datetime  
a = datetime(2016, 8, 30)  
print(a + timedelta(days=10))  
b = datetime(2016, 9, 30)  
d = b - a  
print(d.days)  
  
now = datetime.today()  
print(now)  
print(now + timedelta(minutes=10))
Copy after login

The datetime module can correctly handle leap years

a = datetime(2012, 3, 1)  
b = datetime(2012, 2, 28)  
print((a-b).days)  
c = datetime(2013, 3, 1)  
d = datetime(2013, 2, 28)  
print((c-d).days)
Copy after login

Handle more complex date issues, such as processing time zones and fuzzy time ranges , to calculate the dates of holidays, etc. you can use the dateutil module


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!