Python 3 strptime: Convert String to Datetime Object - Tutorial
The strptime() method is a function in Python's datetime module that converts a string representing a date and time to a datetime object.\n\nHere's an example of how to use strptime(): \n\npython\nfrom datetime import datetime\n\ndate_string = "2021-01-01 12:00:00"\nformat_string = "%Y-%m-%d %H:%M:%S"\n\ndatetime_obj = datetime.strptime(date_string, format_string)\nprint(datetime_obj)\n\n\nIn this example, the strptime() function is used to convert the date_string into a datetime object. The format_string parameter specifies the format of the input string. In this case, %Y represents the four-digit year, %m represents the two-digit month, %d represents the two-digit day, %H represents the hour in 24-hour format, %M represents the minute, and %S represents the second.\n\nThe output of the above code will be:\n\n\n2021-01-01 12:00:00\n\n\nThis shows that the date_string has been successfully converted into a datetime object.
原文地址: https://www.cveoy.top/t/topic/pAMO 著作权归作者所有。请勿转载和采集!