Datetime question

Discussion in 'Python' started by Lad, Aug 3, 2006.

  1. Lad

    Lad Guest

    In a datetime object I would like to change days and hours.
    Or in other words, I would like to copy this datetime object but
    increase days and hours.
    Is it possible?
    For example:If I have a datetime object like this
    datetime.datetime(2006, 8, 3, 14, 13, 56, 609000)

    I would like to make a new ,for example like this

    datetime.datetime(2006, 8, 12, 10, 13, 56, 609000)

    is it possible to do so?
    Thank you
    L
     
    Lad, Aug 3, 2006
    #1
    1. Advertising

  2. In <>, Lad wrote:

    > In a datetime object I would like to change days and hours.
    > Or in other words, I would like to copy this datetime object but
    > increase days and hours.
    > Is it possible?
    > For example:If I have a datetime object like this
    > datetime.datetime(2006, 8, 3, 14, 13, 56, 609000)
    >
    > I would like to make a new ,for example like this
    >
    > datetime.datetime(2006, 8, 12, 10, 13, 56, 609000)
    >
    > is it possible to do so?


    Yes it is, just add a `timedelta` object:

    In [18]: a = datetime.datetime(2006, 8, 3, 14, 13, 56, 609000)

    In [19]: a + datetime.timedelta(days=8, hours=20)
    Out[19]: datetime.datetime(2006, 8, 12, 10, 13, 56, 609000)

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Aug 3, 2006
    #2
    1. Advertising

  3. Lad schrieb:
    > In a datetime object I would like to change days and hours.
    > Or in other words, I would like to copy this datetime object but
    > increase days and hours.
    > Is it possible?
    > For example:If I have a datetime object like this
    > datetime.datetime(2006, 8, 3, 14, 13, 56, 609000)
    >
    > I would like to make a new ,for example like this
    >
    > datetime.datetime(2006, 8, 12, 10, 13, 56, 609000)
    >
    > is it possible to do so?


    you'd been pointed to the resources yesterday - please read manuals
    carefully!

    a = datetime.datetime(2006, 8, 12, 10, 13, 56, 609000)
    b = a + datetime.timedelta(days=-2, hours=-4)


    Diez
     
    Diez B. Roggisch, Aug 3, 2006
    #3
  4. On 8/3/06, Rama <> wrote:
    > But wont this create a new object? Whereas if you want to modify the same
    > object, should we not be using replace? Or does it not matter in the global
    > picture?


    datetime objects are immutable. You can't change the value of an
    existing datetime object, only create a new one.

    --
    Cheers,
    Simon B,
    ,
    http://www.brunningonline.net/simon/blog/
     
    Simon Brunning, Aug 3, 2006
    #4
  5. On 8/3/06, Rama <> wrote:
    > Just curious why when
    > I call id(a) I get the same id after I call the replace method.


    In your example, you called a's replace() method, but did nothing with
    the new datetime object that it returned. The original object, a,
    naturally still has the same ID, but it also still has the same value.

    --
    Cheers,
    Simon B,
    ,
    http://www.brunningonline.net/simon/blog/
     
    Simon Brunning, Aug 3, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    765
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    602
    Tim Peters
    Sep 9, 2003
  3. mp
    Replies:
    1
    Views:
    442
    John Machin
    Jul 28, 2006
  4. Martin
    Replies:
    0
    Views:
    377
    Martin
    Dec 27, 2008
  5. Replies:
    2
    Views:
    803
    M.-A. Lemburg
    Jan 6, 2009
Loading...

Share This Page