简体   繁体   English

python以日期格式解析字符串以获取日期

[英]python parse string in date format to get the date

There is a string and a date format. 有一个字符串和一个日期格式。 I want to get the date based on format. 我想根据格式获取日期。

If date format is YYYY.MM.dd and string is 2017.01.01 . 如果日期格式为YYYY.MM.dd且字符串为2017.01.01 It should transform to a valid date object. 它应该转换为有效的日期对象。

How can I find the date. 我如何找到日期。

You can use datetime module something like this : 您可以使用datetime模块,如下所示:

from datetime import datetime
date_object = datetime.strptime('2017.01.01', '%Y.%m.%d') # Converting the given date string into a datetime object. 
formatted_date = date_object.strftime('%c') #User Defined Output Format
print(formatted_date)

This will result in : 这将导致:

Sun Jan  1 00:00:00 2017

You can refer to the documentation here . 您可以在此处参考文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM