Read the directory
- The time stamp
- The current time
- Time difference
- Time and date formatting symbols in Python
- example
Back to the top
The time stamp
The timestamp is the total number of seconds since January 1, 1970 (08:00:00 GMT) to the current time. Also known as Unix Timestamp, it is ubiquitous in the Unix, C world; The common form is floating point numbers followed by milliseconds after the decimal point. Subtracting the two timestamps gives the time interval (in seconds).
case
import time
time1 = time.time()
time.sleep(15)
time2 = time.time()
print time2 - time1Copy the code
Where time.sleep() is the sleep function, in seconds.
Back to the top
The current time
>>> import datetime,time
>>> now = time.strftime("%Y-%m-%d %H:%M:%S"
) >>> print now
2016-04-30 17:02:26 >>> now = datetime.datetime.now()
>>> print nowCopy the code
Back to the top
Time difference
#1 00:00 yesterday to 23:59 yesterday
>>> import datetime
>>> yestoday = datetime.datetime.now() - datetime.timedelta(days=1)
>>> t1 = "%s-00-00-00" % yestoday.strftime("%Y-%m-%d")
>>> t2 = "%s-23-59-59" % yestoday.strftime("%Y-%m-%d")
>>> print 't1', t1
t1 2016-04-29-00-00-00
>>> print 't2', t2
t2 2016-04-29-23-59-59Copy the code
#2 Now 10 hours later
>>> d1 = datetime.datetime.now()
>>> d3 = d1 + datetime.timedelta(hours=10)
>>> d3.ctime()
'Sun May 1 03:09:58 2Copy the code
#3 The number of seconds and subtleties in such a moment
>>> import datetime >>> starttime = datetime.datetime.now() >>> endtime = datetime.datetime.now() >>> starttime = Datetime.datetime.now () >>> endTime = datetime.datetime.now() >>> print endtime-startTime 0:00:07.390988 >>> print (endtime - starttime).seconds 7 >>> print (endtime - starttime).microseconds 390988Copy the code
The timestamp of the file
>>> import os >>> statinfo=os.stat(r"C:/1.txt") >>> statinfo (33206, 0L, 0, 0, 0, 0, 29L, 1201865413, 1201867904, 1201865413).Copy the code
St_atime (access time), st_mtime (modification time), st_ctime (creation time);
> > > statinfo. St_mtime 1201865413.8952832Copy the code
Note: This time is a Linux timestamp that can be converted to an easy-to-understand format:
>>> import time
>>> time.localtime(statinfo.st_ctime)
(2008, 2, 1, 19, 30, 13, 4, 32, 0)Copy the code
Note: 19:30:13 on Feb 1, 2008 (2008-2-1 19:30:13)
Back to the top
Time and date formatting symbols in Python
%y two-digit year (00-99) %Y four-digit year (000-9999) %M month (01-12) % D day (0-31) %H 24-hour hours (0-23) %I 12-hour hours (01-12) %m minutes (00=59) %S S (00-59) %A Local simplified name of the week %A Local full name of the week %B Local simplified name of the month %B Local full name of the month %c Local date and time representation %j day of the year (001-366) % P local a.m. Or P.M. %U number of weeks in a year (00-53) Sunday is the beginning of the week %w week (0-6), Sunday is the beginning of the week %W Number of weeks in a year (00-53) Monday is the beginning of the week %x Local date %x Local time %Z Name of the current time zone %% % itselfCopy the code
example
#! Coding: UTF-8 ''''' date-related operations "" from datetime import datetime from datetime import timedelta import Calendar DATE_FMT = '% Y - % m - % d' DATETIME_FMT = '% % Y - m - H: % d % % m: % S' DATE_US_FMT = '% % % d/m/Y' ' ' ' ' 'format commonly used several parameters Y: 1999 Y: 99 m: mouth 02 12 M : minute 00-59 S : second d : day H : hour ''' def dateToStr(date): ''''' "return datetime.strftime(date, DATETIME_FMT) def strToDate(strdate): ''''' return datetime.strptime(strdate, DATETIME_FMT) def timeElement(): ''''' print 'year: %s month: %s day: 'now = datetime.today() print 'year: %s month: %s day: %s' %(now.year, now.month, now.day) print 'hour: %s minute: %s second: %s' %(now.hour, now.minute, now.second) print 'weekday: %s' %(now.weekday()+1) def timeAdd(): ''''' addition and subtraction of time, Datetime. timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, Weeks]]]]]]]) arguments can be positive or negative. The resulting object can be added or subtracted by multiplying the number and taking the absolute value. "atime = timedelta(days=-1) now = datetime.strptime('2001-01-30 11:01:02', DATETIME_FMT) print now + atime print now - abs(atime) print now - abs(atime)*31 def lastFirday(): today = datetime.today() targetDay = calendar.FRIDAY thisDay = today.weekday() de = (thisDay - targetDay) % 7 res = today - timedelta(days=de) print res def test(): print dateToStr(datetime.today()) print strToDate('2013-01-31 12:00:01') timeElement() timeAdd() lastFirday() if __name__=='__main__': test()Copy the code
The results of
Connected to pydev debugger (build 141.1899)
2016-05-18 10:40:26
2013-01-31 12:00:01
year: 2016 month: 5 day: 18
hour: 10 minute: 41 second: 13
weekday: 3
2001-01-29 11:01:02
2001-01-29 11:01:02
2000-12-30 11:01:02
2016-05-13 10:41:37.001000Copy the code
This article from jihite blog garden, the original link: http://www.cnblogs.com/kaituorensheng/archive/2012/11/06/2757865.html