AD

python time operation

We use the following form to get the current time stamp:
import time
print time.time()

The output is:
1,285,041,535.86
The result is 1970 to the present time, the number of seconds apart
time.time () returns a float, so if need to add or subtract time, then, is a simple floating-point

Example, you can one day be reduced
time.time()-24*60*60 


Other hours, minutes, the addition and subtraction are the same

But this is a series of numbers is not the result we want, we can use time formatting module hours to deal with:
time.localtime (time.time ())

With time.localtime () method, is to format the timestamp to local time.
The output is:
time.struct_time (tm_year = 2010, tm_mon = 7, tm_mday = 19, tm_hour = 22, tm_min = 33, tm_sec = 39, tm_wday = 0, tm_yday = 200, tm_isdst = 0)

Now looks more promising formats into a time we want.
time.strftime('%Y-%m-%d',time.localtime(time.time()))


Finally, time.strftime () method, the long list of information formatted just what we want, and now the results are:
2010-07-19

time.strftime there are many parameters that can give you more free stuff you want the output:
The following is time.strftime parameters:
strftime (format [, tuple]) -> string
The specified struct_time (default is current time), according to the specified format string output
python in the time and date formatting symbols:

% Y year, said double-digit (00-99)
% Y four-digit year representation (000-9999)
% M month (01-12)
% D The day of the month (0-31)
% H 24-hour number of hours (0-23)
% I 12-hour clock hours (01-12)
% M minutes (00 = 59)
% S seconds (00-59)

% A week the name of the local streamline
% A full week of local name
% B month name of the local streamline
% B full month names of local
% C corresponding local date and time indicated that
% J day of year (001-366)
% P AM or PM equivalent of local character
% U week number of year (00-53) for the week beginning Sunday
% W week (0-6), Sunday is the start of week
% W week number of year (00-53) for the week beginning Monday
% X local date that the corresponding
% X local time that the corresponding
% Z time zone name of the current
%%% Number itself
标签: parameters, time time, import time, string output, time and date, local time, floating point, format string, time stamp, timestamp, current time, present time, addition and subtraction, time operation, time print, free stuff, clock hours
分类: Python
时间: 2010-09-21

相关文章

  1. python registry operation

    Because the system always re-install, so would like to be able to have a way to quickly to the development env ...
  2. integration of python time operation

    # -*- coding:UTF-8 ''' Created on 2010-10-20 @author: DOC @ Time the operation summary ''' import datetime imp ...
  3. Introduction to learning --- python reproduced hour

    A preface for the reader this article as soon as possible for experienced programmers into the Python2.x world ...
  4. python connection mysql

    Usually the main programming language is Java, the development is mainly used when Mysql, often to test, debug ...
  5. Some Python programming article online

    newthreading - safer concurrency for Python security concurrency (1 response) http://www.starming.com/index.ph ...
  6. Python encoding settings to share the knowledge that

    Yesterday I wrote a python mysql operation method of the parameters in accordance with the provision of docume ...
  7. Python encoding settings share a bit of knowledge

    Yesterday I wrote a python mysql operation method of the parameters in accordance with the provision of docume ...
  8. Characters on the python file and the operation

    Afternoon met a need that requires the image name after the three figures for the construction of the image in ...
  9. Operation Python, Oracle

    Python as a learn-to-use and powerful without losing the language, at home and abroad often use Python develop ...
  10. common method of operation python file

    python file operations are very convenient, it has many ways to make the file operation is very simple, short, ...
  11. Operation using python Serial ★ ☆ ★

    Another consideration: how to use python to write a serial communication program? http://www.douban.com/group/ ...
  12. Operation of the serial port using python ★ ☆ ★

    Another consideration: how to use python to write a serial communication program? http://www.douban.com/group/ ...
  13. Operation of the registry using Python

    Associated with the registry operation function can be divided into open registry, close the registry, read th ...
  14. VBA Excel documents python operation

    Through the python's win32com interface component called Microsoft's office in order to achieve the read and w ...
  15. MS Access database with a Python operation

    First, you should install the python and Python for Windows extensions. 1, establishing a database connection ...
  16. Using python operation TTServer

    Recently, the company's projects use to TT, TT's in Key-Value the advantages of such a database, there was no ...
  17. Python: file manipulation techniques (File operation)

    Read and write files # ! /usr/bin/python # -*- coding: utf8 -*- spath = " D:/download/baa.txt " f = ...
  18. mongoDB python operation

    win driver here: pymongo-1.9.win32-py2.6.exe # -*- coding:UTF-8 ''' Created on 2010-12-23 @author: DOC ''' fro ...
  19. operation of the python sqlite

    #a demo for coding python with sqlite3 import sqlite3 conn=sqlite3.connect("e:\python.db") print(typ ...