Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Datetime question

Reply
Thread Tools

Datetime question

 
 
Lad
Guest
Posts: n/a
 
      08-03-2006
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

 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      08-03-2006
In <(E-Mail Removed). com>, 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
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      08-03-2006
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
 
Reply With Quote
 
Simon Brunning
Guest
Posts: n/a
 
      08-03-2006
On 8/3/06, Rama <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed),
http://www.brunningonline.net/simon/blog/
 
Reply With Quote
 
Simon Brunning
Guest
Posts: n/a
 
      08-03-2006
On 8/3/06, Rama <(E-Mail Removed)> 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,
(E-Mail Removed),
http://www.brunningonline.net/simon/blog/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: [2.4.4] creating a datetime.datetime from an XML xs:dateTime skip@pobox.com Python 2 01-06-2009 01:31 PM
[2.4.4] creating a datetime.datetime from an XML xs:dateTime Martin Python 0 12-27-2008 08:08 PM
mx.DateTime to datetime.datetime mp Python 1 07-28-2006 10:57 PM
datetime: .datetime-.datetime = .timedelta, .time-.time=TypeError ? Christos TZOTZIOY Georgiou Python 3 09-13-2003 10:44 AM
RE: datetime: .datetime-.datetime = .timedelta, .time-.time=TypeError ? Tim Peters Python 0 09-09-2003 12:57 AM



Advertisments