No matter when and where, as long as we are programmed to encounter time-related problems, we must think of datetime and the standard library module, today we use its internal methods, detailed Python operation date and time method.
1. Convert the time of a string to a timestamp
Copy Code code as follows:
Method:
A = "2013-10-10 23:40:00"
#将其转换为时间数组
Import time
Timearray = Time.strptime (A, "%y-%m-%d%h:%m:%s")
#转换为时间戳:
timeStamp = Int (time.mktime (Timearray))
TimeStamp = 1381419600
2. Format Change
If a = "2013-10-10 23:40:00", want to change to a = "2013/10/10 23:40:00"
Methods: Convert to a time array and then convert to another format
Copy Code code as follows:
Timearray = Time.strptime (A, "%y-%m-%d%h:%m:%s")
Otherstyletime = Time.strftime ("%y/%m/%d%h:%m:%s", Timearray)
3. Time stamp conversion to the specified format date
Method One: Use LocalTime () to convert to a time array, and then format it in the format you want, such as:
Copy Code code as follows:
TimeStamp = 1381419600
Timearray = Time.localtime (TimeStamp)
Otherstyletime = Time.strftime ("%y-%m-%d%h:%m:%s", Timearray)
Otherstyletime = "2013-10-10 23:40:00"
Method Two:
Copy Code code as follows:
Import datetime
TimeStamp = 1381419600
Datearray = Datetime.datetime.utcfromtimestamp (TimeStamp)
Otherstyletime = Datearray.strftime ("%y-%m-%d%h:%m:%s")
Otherstyletime = "2013-10-10 23:40:00"
4. Get the current time and convert to the specified date format
Method One:
Copy Code code as follows:
Import time
#获得当前时间时间戳
now = Int (Time.time ())-> This is a timestamp
#转换为其他日期格式, such as: "%y-%m-%d%h:%m:%s"
Timearray = Time.localtime (TimeStamp)
Otherstyletime = Time.strftime ("%y-%m-%d%h:%m:%s", Timearray)
Method Two:
Copy Code code as follows:
Import datetime
#获得当前时间
now = Datetime.datetime.now ()-> This is the time array format
#转换为指定的格式:
Otherstyletime = Now.strftime ("%y-%m-%d%h:%m:%s")
5. Method of obtaining the time three days ago
Copy Code code as follows:
Import time
Import datetime
#先获得时间数组格式的日期
Threedayago = (Datetime.datetime.now ()-Datetime.timedelta (days = 3))
#转换为时间戳:
timeStamp = Int (Time.mktime (Threedayago.timetuple ()))
#转换为其他字符串格式:
Otherstyletime = Threedayago.strftime ("%y-%m-%d%h:%m:%s")
Note: The parameters of Timedelta () are: days,hours,seconds,microseconds
6. A given timestamp, a few days before the time was calculated
Copy Code code as follows:
TimeStamp = 1381419600
#先转换为datetime
Import datetime
Import time
Datearray = Datetime.datetime.utcfromtimestamp (TimeStamp)
Threedayago = Datearray-datetime.timedelta (days = 3)
#参考5, you can convert to any other format.
7, using Python to calculate the date of yesterday and tomorrow
Copy Code code as follows:
>>> Import datetime #导入日期时间模块
>>> today = Datetime.date.today () #获得今天的日期
>>> Print Today #输出今天日期
2014-01-04
>>> yesterday = Today-datetime.timedelta (Days=1) #用今天日期减掉时间差, parameter is 1 days, get yesterday's date
>>> Print yesterday
2014-01-03
>>> tomorrow = Today + Datetime.timedelta (Days=1) #用今天日期加上时间差, parameter is 1 days, get tomorrow's date
>>> Print tomorrow
2014-01-05
>>>
>>> print "Yesterday:%s, today:%s, tomorrow:%s"% (Yesterday, today, tomorrow) #字符串拼接在一起输出, this 3-day date
Yesterday: 2014-01-03, today: 2014-01-04, Tomorrow: 2014-01-05
8. Use the time module in Python to get the current date
Copy Code code as follows:
#!/usr/bin/python
Import time
Print (Time.strftime ("%h:%m:%s"))
# Hour Format # #
Print (Time.strftime ("%i:%m:%s"))
#: Output
#18:11:30
#6:11:30
9, print out the current date of the Python program
Copy Code code as follows:
!/usr/bin/python
Import time
# # DD/MM/YYYY Format
Print (Time.strftime ("%d/%m/%y"))
#输出:
11/03/2014
10, use the DateTime module to get the current date and time
Copy Code code as follows:
#!/usr/bin/python
Import datetime
i = Datetime.datetime.now ()
Print ("Current date and time is%s"% i)
Print ("Date and time in ISO format is%s"% I.isoformat ())
Print ("Current year is%s"%i.year)
Print ("Current month is%s"%i.month)
Print ("Current date is%s"%i.day)
Print ("dd/mm/yyyy format is%s/%s/%s"% (I.day, I.month, I.year))
Print ("Current hour is%s"%i.hour)
Print ("The current minute is%s"%i.minute)
Print ("Current seconds is%s"%i.second)
attached: Format parameters for date and time
Copy Code code as follows:
%a Day of the week
%A the full name of the week
%b of the Month
Full name of%B month
Time series for the%c standard date
Rear two digits of%c year
The first day of the month in%d decimal notation
%d Month/day/year
%e in a two-character field, the first day of the month in decimal notation
%F year-month-day
Two digits of%g year, using week-based years
%G years, using weeks based
%h Abbreviated month name
%H 24-hour system hours
%I 12-hour hours
%j decimal representation of the first day of the year
Month in%m decimal notation
Number of minutes represented by%m 10 o'clock
%n New Line character
%p the equivalent display of a local AM or PM
%r 12 hours.
%R display hours and minutes: hh:mm
%s decimal number of seconds
%t Horizontal Tab
%T when displayed: Hh:mm:ss
%u days of the week, Monday is the first day (values from 0 to 6, Monday to 0)
%u week of the year, make Sunday the first day (values from 0 to 53)
%V the first weeks of the year, using a week based year
%w decimal representation of the week (values from 0 to 6, Sunday to 0)
%w Week of the year, Monday is the first day (value from 0 to 53)
The date string of the%x standard
%x Standard Time series
%y decimal Year without century (values from 0 to 99)
%Y 10 year of the century part
%z,%z the time zone name and returns a null character if the time zone name cannot be obtained.
Percent percent%