0

I'm trying to build a function that recieves a date and adds days, updating everything in case it changes, so far i've come up with this:

def addnewDate(date, numberOfDays):

    date = date.split(":")   
    day = int(date[0])
    month = int(date[1])
    year = int(date[2])
    new_days = 0
    l = 0
    l1 = 28
    l2 = 30
    l3 = 31
    #l's are the accordingly days of the month

    while numberOfDays > l:
        numberOfDays  = numberOfDays - l 
        if month != 12:
            month += 1
        else:
            month = 1
            year += 1

        if month in [1, 3, 5, 7, 8, 10, 12]:
            l = l3
        elif month in [4, 6, 9, 11]:
            l = l2
        else:
            l = l1

    return  str(day) + ':' + str(month) + ':' + str(year) #i'll deal 
    #with fact that it doesn't put the 0's in the < 10 digits later

Desired output:

addnewDate('29:12:2016', 5):

'03:01:2017'

I think the problem is with either the variables, or the position i'm using them in, kinda lost though..

Thanks in advance!

p.s I can't use python build in functions :)

2
  • sometimes l1 = 29 (every 4 year but not divisible by 400) Commented Dec 15, 2016 at 21:12
  • Oh ye, forgot bout that, but for the sake of simplicity, let's assume it only goes to 28 Commented Dec 15, 2016 at 21:16

1 Answer 1

1

Since you cannot use standard library, here's my attempt. I hope I did not forget anything.

  • define a table for month lengths
  • tweak it if leap year detected (every 4 year, but special cases)
  • work on zero-indexed days & months, much easier
  • add the number of days. If lesser that current month number of days, end, else, substract current month number of days and retry (while loop)
  • when last month reached, increase year
  • add 1 to day and month in the end

code:

def addnewDate(date, numberOfDays):
    month_days = [31,28,31,30,31,30,31,31,30,31,30,31]

    date = date.split(":")
    day = int(date[0])-1
    month = int(date[1])-1
    year = int(date[2])
    if year%4==0 and year%400!=0:
        month_days[1]+=1

    new_days = 0
    #l's are the accordingly days of the month

    day += numberOfDays

    nb_days_month = month_days[month]

    done = False   # since you don't want to use break, let's create a flag
    while not done:
        nb_days_month = month_days[month]
        if day < nb_days_month:
            done = True
        else:
            day -= nb_days_month
            month += 1
            if month==12:
                year += 1
                month = 0


return  "{:02}:{:02}:{:04}".format(day+1,month+1,year)

test (may be not exhaustive):

for i in ("28:02:2000","28:02:2004","28:02:2005","31:12:2012","03:02:2015"):
    print(addnewDate(i,2))
    print(addnewDate(i,31))

result:

02:03:2000
31:03:2000
01:03:2004
30:03:2004
02:03:2005
31:03:2005
02:01:2013
31:01:2013
05:02:2015
06:03:2015

of course, this is just for fun. Else use time or datetime modules!

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the quick reply, i'll look into it but i don't think i'll be able to use it fully at least, i can't use built in modules from python because i'ts a school work, therefor can't use break either..
okay, that's stupid, but whatever, check my edit. No trace of break anymore. You teacher should know better...
I'll check it out :) this is for a task assignemnt to deliveries, half of the code i posted was advised by him, so i'm kinda trying to make that work since it should have able to work (also for the future if i need it) to add stupid stuff like 400 days, thanks for your help
sorry to bother you again, i'm probably gonna use your code (!) but i'm not understanding very well why you subtracted one then added it at the end. Also correct me if i'm wrong, the 13rd date at the list is because of the 0 month when the year changes right?
I found it easier to work 0-indexed, then restore the 1 indexed in the end. specially when you compare with numbers of days. and the list is too long. Editing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.