Home > Article > Backend Development > How to get the current system time in python
Python obtains the current system time, including year, month, day, hours, minutes and seconds, mainly through the datetime module in Python.
Recommended tutorial: "Python Tutorial"
Below we will use specific code examples to give you a detailed introduction to Python to obtain the current How to implement time and date.
The code example is as follows:
import datetime now = datetime.datetime.now() print ("当前系统日期和时间是: ") print (now.strftime("%Y-%m-%d %H:%M:%S"))
Get the current system time, the result is as follows:
Python datetime :
The datetime module provides classes for manipulating dates and times in simple and complex ways.
datetime includes the following classes:
date-date object, commonly used attributes are year, month, day
time-time object
datetime-date Time object, commonly used attributes include hour, minute, second, microsecond
datetime_CAPI-Date time object C language interface
timedelta-time interval, that is, the length between two time points
tzinfo - Time zone information object.
now(tz=None)Returns the current local date and time. If the optional parameter tz is not specified, it is the same as today().
strftime(format)Returns a string representing a date, controlled by an explicit format string. Format codes referencing hours, minutes, or seconds will see a value of 0.
This article is an introduction to the method of obtaining the current system time and date in Python. It is also very simple and easy to understand. I hope it will be helpful to friends in need!
The above is the detailed content of How to get the current system time in python. For more information, please follow other related articles on the PHP Chinese website!