SoFunction
Updated on 2025-03-01

Python date operation learning notes

For example, use print ','.join(datelist)
You can combine all the items in the datelist list into a string. Of course, this expression will insert a comma in the middle of each item, which is simpler than using a loop.
The operation of date must use the time or datetime library
import time
>>> s="2006-1-2"
>>> (s,"%Y-%m-%d)
This is to convert the date and time in string format into a date object
The corresponding meaning of the escape character is as follows
%a Local simplified week name
%A Local full week name
%b Locally simplified month name
%B Local full month name
%c Local corresponding date and time representation
%d One day in the month (0-31)
%H 24-hour hours (0-23)
%I 12-hour hours (01-12)
%j One day in the year (001-366)
%m Month (01-12)
%M Minutes (00=59)
%p local .or. equivalence
%S seconds (00-59)
%U The number of weeks of the year (00-53) Sunday is the beginning of the week
%w Week (0-6), Sunday is the beginning of the week
%W The number of weeks of the year (00-53) Monday is the beginning of the week
%x local corresponding date
%X local corresponding time representation
%y Double digit year representation (00-99)
%Y Four-digit year representation (000-9999)
%Z The name of the current time zone
%% % % number itself
============================================
#-*- coding:utf-8 -*-
import time
import datetime
# 2007-11-25 15:36:35
#Using the datetime module can easily solve this problem, for example:
d1 = (2005, 2, 16)
d2 = (2004, 12, 31)
# Results: 47
print (d1 - d2).days
#The above example demonstrates the calculation of the number of days that are different from two dates.
starttime = ()
endtime = ()
print (endtime - starttime).seconds
#The above example demonstrates an example of calculating runtime, displayed in seconds.
d1 = ()
d3 = d1 + (days =10)
print str(d3)
print ()
# The above example demonstrates the calculation of the time 10 days later.
# If it is an hour, change to hours
# The commonly used classes are: datetime and timedelta. They can be added or subtracted.
# Each class has some methods and properties to view specific values, such as datetime to view: days (day), hours (hour), weekday (weekday()), etc.;
# timedelta can be viewed: days, seconds, etc.
#
# time , datetime , string types convert each other
#
# string -> time
# (publishDate,"%Y-%m-%d %H:%M:%S")
#
# time -> string
# ("%y-%m-%d",t)
date = '2007-01-01'
print type(date)
date = (date,"%Y-%m-%d")
print type(date)
print date[0]
d4 = (date[0], date[1],date[2])
print d4
print type(d4)
#Convert date and time object into strings to use
date = ("%y-%m-%d",date)
print type(date)
# where d is the date and time object