Python字符串转换为日期时间– strptime()

We can convert a string to datetime using strptime() function. This function is available in datetime and time modules to parse a string to datetime and time objects respectively.

我们可以使用strptime()函数将字符串转换为datetime。 datetime和time模块中提供了此功能,可分别将字符串解析为datetime和time对象。

Python strptime() (Python strptime())

Python strptime() is a class method in datetime class. Its syntax is:

Python strptime()是datetime类中的类方法。 其语法为:

datetime.strptime(date_string, format)

Both the arguments are mandatory and should be string. This function is exactly opposite of strftime() function, which converts datetime object to a string.

这两个参数都是强制性的,应为字符串。 此函数与strftime()函数完全相反,该函数将datetime对象转换为字符串。

We have the similar function available in time module too, where its syntax is:

我们在时间模块中也提供了类似的功能,其语法为:

time.strptime(time_string[, format])

Here the function returns struct_time object. If format string is not provided, it defaults to “%a %b %d %H:%M:%S %Y” which matches the formatting returned by ctime() function.

在这里,该函数返回struct_time对象。 如果未提供格式字符串,则默认为“%a%b%d%H:%M:%S%Y”,它与ctime()函数返回的格式匹配。

If the input string cannot be parsed according to the provided format, then ValueError is raised. The exception message provides clear details about the issue in parsing.

如果无法根据提供的格式解析输入字符串,则引发ValueError 。 异常消息提供了有关解析中问题的清晰详细信息。

Python strptime()格式指令 (Python strptime() format directives)

Following table contains most of the commonly used format directives.

下表包含大多数常用的格式指令。

你可能感兴趣的:(字符串,python,javascript,编程语言,人工智能,ViewUI)