Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading