how do you manipulate the days on a calender

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • roryclancy
    New Member
    • Jan 2014
    • 8

    how do you manipulate the days on a calender

    when I did this on hypercard all i had to do was:

    put the date into date
    convert date to secs(onds)
    date =+ (24*60*60*No_of _days_i_want_to _add_on)
    convert date to the date

    Is there any way I could do that with Python
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Here's an example of using built-in modules datetime and time to parse a date string and add days:
    Code:
    import datetime as dt
    import time
    
    userDate = "2007/08/20"
    timeObj = time.strptime(userDate, "%Y/%m/%d")
    daysToAdd = 15
    dateObj = dt.datetime(*timeObj[:6])
    
    print dateObj
    print dateObj + dt.timedelta(days=daysToAdd)

    Comment

    • roryclancy
      New Member
      • Jan 2014
      • 8

      #3
      this works great thank you, you got me out of a bind again, I've looked it over and messed with it to see how it works, but I can't figer out why the
      Code:
      [:6]
      as in
      Code:
      dateObj = dt.datetime(*timeObj[:6])
      also what is dt as in
      Code:
      import datetime as dt

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        The time object has additional attributes that would not be accepted by the datetime.dateti me constructor. That's why I take a slice of the time object ([:6]) which returns the first 6 elements. It works because a time object is iterable.

        "import datetime as dt" is a way to abbreviate datetime.
        Code:
        >>> for attr in timeObj:
        ... 	print attr
        ... 	
        2007
        8
        20
        0
        0
        0
        0
        232
        -1
        >>> timeObj[:6]
        (2007, 8, 20, 0, 0, 0)
        >>>

        Comment

        • roryclancy
          New Member
          • Jan 2014
          • 8

          #5
          very cool

          thanks again

          Comment

          Working...