'How to use parser on multiple time objects
I have an list:
list = ['2022-06-01', '2022-02-02']
Now am using parser to convert this to python date object. like this,
from dateutil import parser
def to_date(value):
for data in value:
return parser.parse(data)
Above one gives just one output, but i need the output for both the times and also need to convert that to an string like this:
From : June 1, 2022 | To: Feb. 28, 2022
Is that possible with parser ?
Solution 1:[1]
You can use the standard datetime
library to perform the parsing.
datetime.datetime.strptime
allows you to convert a datetime string to a datetime object.
datetime.datetime.strftime
allows you to convert a datetime object to the desired string.
dt_from, dt_to = [datetime.datetime.strptime(x, "%Y-%m-%d") for x in dt_list]
dt_str = f"From : {datetime.datetime.strftime('%b %d,%Y', dt_from)} | To: {datetime.datetime.strftime('%b %d,%Y', dt_to)}"]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | DeGo |