No matter when and where, as long as we are programmed to encounter time-related problems, we have to think of the datetime and the standard library module, today we will use its internal methods, detailed Python operation date and time method.
1. Convert the time of a string to a timestamp
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. Formatting changes
If a = "2013-10-10 23:40:00", want to change to a = "2013/10/10 23:40:00"
Method: Convert to a time array first and then to another format
Timearray = Time.strptime (A, "%y-%m-%d%h:%m:%s")
Otherstyletime = Time.strftime ("%y/%m/%d%h:%m:%s", Timearray)
3. Timestamp is converted to the specified format date
Method One: Use LocalTime () to convert to a time array, and then format to the desired format, such as:
The code is 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
The code is 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:The code is as follows:Import time
#获得当前时间时间戳
now = Int (Time.time ())--This is the 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:The code is as follows:Import datetime
#获得当前时间
now = Datetime.datetime.now ()-This is the time array format
#转换为指定的格式:
Otherstyletime = Now.strftime ("%y-%m-%d%h:%m:%s")
5. How to get the time three days agoThe code is 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. Given a timestamp, calculate the time of the day before the timeThe code is as follows:TimeStamp = 1381419600
#先转换为datetime
Import datetime
Import time
Datearray = Datetime.datetime.utcfromtimestamp (TimeStamp)
Threedayago = Datearray-datetime.timedelta (days = 3)
#参考5, it can be converted to any other format.
7. Use Python to calculate yesterday's and tomorrow's datesThe code is 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 timesThe code is 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 programThe code is 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 timeThe code is 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 ("Current minute is%s"%i.minute)
Print ("Current seconds is%s"%i.second)
attached: Date and time formatting parametersThe code is as follows:
%a shorthand for the day of the week
%A full name of the week
%b of the Month
Full name of the%B month
Time string for the date of%c standard
After two digits of the%c year
The day ordinal of a month in%d decimal notation
%d Month/day/year
%e the day ordinal of a month in a two-character field, in decimal notation
%F year-month-day
%g two digits of the year, using week-based
%G years, using week-based years
%h Abbreviated month name
%H 24-Hour Hour
%I 12-Hour Hour
%j decimal indicates the day ordinal of the year
%m the month represented by decimal
%M minutes in a 10 o'clock-hour representation
%n New Line character
%p equivalent display of the local AM or PM
%r 12 hours of time
%R display hours and minutes: hh:mm
%s number of seconds in decimal
%t Horizontal Tab
%T display time seconds: hh:mm:ss
%u days of the week, Monday for the first day (values from 0 to 6, Monday to 0)
%u year of the week, the Sunday as the first day (value from 0 to 53)
%V Week of the year, using week-based
%w Decimal Day of the week (value from 0 to 6, Sunday is 0)
%W Week of the year, Monday as the first day (value from 0 to 53)
Date string for%x standard
Time string for%x standard
%y decimal Year without century (values from 0 to 99)
%Y 10 year with century part
%z,%z the time zone name and returns a null character if the time zone name cannot be obtained.
Percent hundred percent semicolon
Python how to manipulate date and time