SoFunction
Updated on 2024-11-15

python programming development of date operation example analysis

This article example describes the date operation of python programming and development. Shared for your reference, as follows:

The libraries that operate on dates in python are:

import datetime
import time

For date formatting information, you can refer to the official API:

datetime

Here is the demo I made:

#datetime
import datetime
# Current date
now = ()
print(('%Y-%m-%d %H:%M:%S'))
print(('%Y-%m-%d'))
#string convert to datetime
time_str = '2013-07-29 01:05:00'
str_convert_2_time = (time_str, '%Y-%m-%d %H:%M:%S')
print(str_convert_2_time)
# Compare how many days apart two dates are
time_strA = '2013-07-29 01:05:00'
time_strB ='2013-08-29 01:05:00' 
day = (time_strA, '%Y-%m-%d %H:%M:%S')
day2 = (time_strB, '%Y-%m-%d %H:%M:%S')
sub_day = day2 - day
print('{0}cap (a poem){1}discrepancy between{2}sky'.format(time_strA, time_strB, str(sub_day.days)))
# Dates for the next n days
n_days = 4
now = ()
my_date = (days=n_days) 
n_day = now + my_date
print('从今sky起的{0}sky的日期是:'.format(n_days))
print(n_day.strftime('%Y-%m-%d %H:%M:%S'))

Running effects:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
2013-07-29 01:48:16
2013-07-29
2013-07-29 01:05:00
2013-07-29 01:05:00respond in singing2013-08-29 01:05:00discrepancy between31sky
从今sky起的4sky的日期是:
2013-08-02 01:48:16
>>>

I hope that what I have said in this article will help you in Python programming.