📅  最后修改于: 2023-12-03 14:48:41.963000             🧑  作者: Mango
In programming, it is common to work with dates and times. In Python, the built-in datetime module provides a set of classes for working with dates and times. The format yyyy-mm-dd hh:mm:ss.0
represents a datetime object in Python.
To create a datetime object with the format yyyy-mm-dd hh:mm:ss.0
, you can use the datetime.strptime
method. For example:
from datetime import datetime
date_string = "2022-08-30 09:15:00.0"
date_obj = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S.%f")
print(date_obj)
This will output:
2022-08-30 09:15:00
Note that the format argument %f
is used to indicate the fractional seconds.
Once you have a datetime object, you can perform various operations on it. For example, you can format it as a string using the strftime
method:
date_str = date_obj.strftime("%Y-%m-%d %H:%M:%S.%f")
print(date_str)
This will output:
2022-08-30 09:15:00.000000
You can also perform arithmetic operations on datetime objects, such as adding or subtracting a certain amount of time:
from datetime import timedelta
one_hour = timedelta(hours=1)
new_date_obj = date_obj + one_hour
print(new_date_obj)
This will output:
2022-08-30 10:15:00
In summary, the format yyyy-mm-dd hh:mm:ss.0
represents a datetime object in Python. You can create and manipulate datetime objects using the built-in datetime module.