What is the module/method used to get the current time?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
By default,
now()
function returns output in theYYYY-MM-DD HH:MM:SS:MS
format. Use the below sample script to get the current date and time in a Python script and print results on the screen. Create filegetDateTime1.py
with the below content.The output looks like below:
If you just want the current timestamp in ms (for example, to measure execution time), you can also use the "timeit" module:
That outputs the current GMT in the specified format. There is also a localtime() method.
This page has more details.
date will print date and time will print time.
This is what I ended up going with:
Also, this table is a necessary reference for choosing the appropriate format codes to get the date formatted just the way you want it (from Python "datetime" documentation here).
For this example, the output will be like this:
'2013-09-18 11:16:32'
Here is the list of strftime.