Datetime question

L

Lad

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
 
M

Marc 'BlackJack' Rintsch

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
 
D

Diez B. Roggisch

Lad said:
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
 
S

Simon Brunning

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.
 
S

Simon Brunning

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top