简体   繁体   中英

How to format date in this format to yyyymmdd

dat = df['Date'].tolist()
['Sun 11  - Thu 15 Apr 2021      \u2002Online Edition Available',
 'Tue 27  - Thu 29 Apr 2021',
 'Fri 14  - Sat 15 May 2021',
 'Mon 24  - Thu 27 May 2021',
 'Tue 13  - Sat 17 Apr 2021',
 'Fri 23  - Sun 25 Apr 2021',
 'Sun 13  - Tue 15 Jun 2021',
 'Wed 07  - Sun 18 Apr 2021']

Want to split this output into start_date and end_date in yyyymmdd format

Here is a solution assuming each start_date and end_date occur in the same month and the formatting is always the same. You can adapt it in case the formatting change.

import datetime
import pandas as pd

start_date_list = list()
end_date_list = list()
for index, row in data_df.iterrows():
    date_list = row['Date'].split(' - ')
    end_date = datetime.datetime.strptime(date_list[1], '%a %d %b %Y')
    start_date = datetime.date(end_date.year, end_date.month, int(date_list[0].split(' ')[1]))

    start_date_list.append(start_date.strftime('%Y%m%d'))
    end_date_list.append(end_date.strftime('%Y%m%d'))

data_df['start_date'] = start_date_list
data_df['end_date'] = end_date_list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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