简体   繁体   中英

Calculating the weeks between two dates in python from strings that have different formats

Data={
    0:{
        'date': "12 jul 2021",
        'country': "xyz",
        'country_id': "0",
        'event_dict': {
           0: {
               'event1': "T",
               'event_no': "45",
               'event_id': "01"
           },
           1: {
               'event1': "C",
               'event_no': "32",
               'event_id': "T",
           },    
           2: {
              'event1': "B",
              'event_no': "11",
              'event_id': "s",
           },    
           3: {
              'event1': "A",
              'event_no': "0",
              'event_id':"p",
           }
        },      
    },    
   1:{        
       'date1': "20th jun 2022",
       'country': "",
       'country_id': "1",    
       'event_dict': {
           0:{
              'event1': "no",
              'event_no': "23",
              'event_id':"abc"
           },
           1: {
              'event1': "yes",
              'event_no': "8",
              'event_id':"def",
           },    
           2: {
              'event1': "false",
              'event_no': "",
              'event_id': "ghi",
           },    
           3: {
              'event1': "NA",
              'event_no': "9",
              'event_id': "jkl", 
           }
        },
    },
}


res = Data[0]['date']
print(res)
# output is 12 jul 2021

res2 = Data[1]['date1']
print(res2)
# output is 20th jun 2022

I want to get the difference of weeks for this two dates that I have stored in two different variable. How to do this?

First, you need to convert string to datetime with specific format of each date then compute different like below.

from datetime import datetime

d1 = datetime.strptime(Data[0]['date'], '%d %b %Y') # date is 12 jul 2021
d2 = datetime.strptime(Data[1]['date1'], '%dth %b %Y') # date is 20th jun 2022
diff = d2 - d1
print(f'days : {diff.days}')
print(f'weeks : {diff.days//7}')

days : 343
weeks : 49

Other formats for dates if you need to use and maybe your dates have different formats: (From here : Ref )

  • %d is the day number (2 digits, prefixed with leading zero's if necessary)
  • %m is the month number (2 digits, prefixed with leading zero's if necessary)
  • %b is the month abbreviation (3 letters)
  • %B is the month name in full (letters)
  • %y is the year number abbreviated (last 2 digits)
  • %Y is the year number full (4 digits)

You'll need to convert the dates into a date object first. At the moment they're strings. You can do that by using something similar to this:

from datetime import datetime

date_time_str = '20th june 2022'

date_time_obj = datetime.strptime(date_time_str, '%dth %B %Y')


print ("The type of the date is now",  type(date_time_obj))
print ("The date is", date_time_obj)

Once you have done this, you can subtract the two dates to get the days between them. Then you will just need to convert the days difference into a week difference.

res2 = res2.replace("th", "") # sanitize date string.

res = datetime.strptime(res, "%d %b %Y") # parse first date string to date
res2=datetime.strptime(res2, "%d %b %Y") # parse second date string to date

print (res2-res) # date substraction gives you days.

dayDiff = (res2-res).days # get the days
print (dayDiff/7) # and divide by 7 to get weeks

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