How to use the datetime module to get the current date and time in Python 3.x

WBOY
Release: 2023-07-31 15:33:43
Original
2451 people have browsed it

How to use the datetime module to obtain the current date and time in Python 3.x

In Python programming, the datetime module can be used to easily obtain the current date and time. The datetime module provides various functions and classes to handle dates, times, and intervals.

In order to use the datetime module, you first need to import the module:

import datetime
Copy after login

The way to get the current date and time is to use the datetime class in the datetime module. This class contains many useful methods for working with dates and times.

To get the current date and time, you can use the now() method of the datetime class:

current_datetime = datetime.datetime.now() print("当前日期和时间:", current_datetime)
Copy after login

Running this code will output the current date and time. The output format is usually YYYY-MM-DD HH:MM:SS.ssssss, where YYYY represents the four-digit year, MM represents the two-digit month, DD represents the two-digit date, and HH represents the hour in the 24-hour format , MM represents minutes, SS represents seconds, and ssssss represents microseconds.

To get the current date, you can use the date() method of the datetime class:

current_date = current_datetime.date() print("当前日期:", current_date)
Copy after login

Run this code and the current date will be output. The output format is usually YYYY-MM-DD.

To get the current time, you can use the time() method of the datetime class:

current_time = current_datetime.time() print("当前时间:", current_time)
Copy after login

Run this code and the current time will be output. The output format is usually HH:MM:SS.ssssss.

In addition to the now() method, the datetime class has many other useful methods that can be used to handle dates and times. Here are some examples of commonly used methods:

# 获取当前年份 year = current_datetime.year print("当前年份:", year) # 获取当前月份 month = current_datetime.month print("当前月份:", month) # 获取当前日期 day = current_datetime.day print("当前日期:", day) # 获取当前小时 hour = current_datetime.hour print("当前小时:", hour) # 获取当前分钟 minute = current_datetime.minute print("当前分钟:", minute) # 获取当前秒钟 second = current_datetime.second print("当前秒钟:", second) # 获取当前微秒 microsecond = current_datetime.microsecond print("当前微秒:", microsecond)
Copy after login

Running this code will output the current year, month, date, hour, minute, second and microsecond.

Use the datetime module to easily obtain the current date and time, and perform various operations on date and time. With the above sample code, you can easily get the current date and time in Python 3.x and perform further processing.

The above is the detailed content of How to use the datetime module to get the current date and time in Python 3.x. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!