473,231 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

confused on calculating date difference in days.

hello,
I am strangely confused with a date calculation problem.
the point is that I want to calculate difference in two dates in days.
there are two aspects to this problem.
firstly, I can't get a way to convert a string like "1/2/2005" in a
genuan date object which is needed for calculation.
now once this is done I will create a another date object with
today = datetime.datetime.now()
and then see the difference between this today and the string that I
converted to date.
now in the first place I can't recall how I can convert a string to a date.
then now I don't know how to calculate difference in days between
today and the string converted date.
any help will be appreciated.
regards,
Krishnakant.
Oct 16 '07 #1
6 8977
On Tue, 16 Oct 2007 12:33:33 +0530, krishnakant Mane wrote:
firstly, I can't get a way to convert a string like "1/2/2005" in a
genuan date object which is needed for calculation.
Why? Split the string up, convert the parts to `int` and just create a
`datetime.date` object.
now once this is done I will create a another date object with
today = datetime.datetime.now()
and then see the difference between this today and the string that I
converted to date.
now in the first place I can't recall how I can convert a string to a date.
then now I don't know how to calculate difference in days between
today and the string converted date.
In [421]: '1/2/2005'.split('/')
Out[421]: ['1', '2', '2005']

In [422]: map(int, '1/2/2005'.split('/'))
Out[422]: [1, 2, 2005]

In [423]: month, day, year = map(int, '1/2/2005'.split('/'))

In [424]: a = datetime.date(year, month, day)

In [425]: b = datetime.date.today() - a

In [426]: b.days
Out[426]: 1017

Maybe you should read the docs next time. ;-)

Ciao,
Marc 'BlackJack' Rintsch
Oct 16 '07 #2
"krishnakant Mane" <re**********@gmail.comwrites:
firstly, I can't get a way to convert a string like "1/2/2005" in a
genuan date object which is needed for calculation.
Until recently, this was a wart in the Python standard library:
datetime objects exist in the 'datetime' module, but parsing strings
to get date/time components was only available in the 'time' module.

In Python 2.5, though, we have 'datetime.datetime.strptime'
<URL:http://docs.python.org/lib/datetime-datetime.html#l2h-634>, which
parses a string according to a specified format, and returns the
corresponding datetime value.
>>import datetime
datetime.datetime.strptime("2007-10-16t17:40:00", "%Y-%m-%dt%H:%M:%S")
datetime.datetime(2007, 10, 16, 17, 40)
now once this is done I will create a another date object with
today = datetime.datetime.now()
and then see the difference between this today and the string that I
converted to date.
That's simple: datetime objects support difference via the subtraction
arithmetic operator, returning a datetime.timedelta instance
<URL:http://docs.python.org/lib/datetime-timedelta.html>.
>>value = datetime.datetime(2007, 10, 16, 15, 30, 45)
value - datetime.datetime(2007, 10, 16, 15, 20, 00)
datetime.timedelta(0, 645)
>>value - datetime.datetime(2007, 10, 12, 8, 25, 19)
datetime.timedelta(4, 25526)
>>value - datetime.datetime(2007, 11, 26, 0, 0, 0)
datetime.timedelta(-41, 55845)

--
\ "Experience is that marvelous thing that enables you to |
`\ recognize a mistake when you make it again." -- Franklin P. |
_o__) Jones |
Ben Finney
Oct 16 '07 #3
Marc 'BlackJack' Rintsch <bj****@gmx.netwrites:
On Tue, 16 Oct 2007 12:33:33 +0530, krishnakant Mane wrote:
firstly, I can't get a way to convert a string like "1/2/2005" in
a genuan date object which is needed for calculation.

Why? Split the string up, convert the parts to `int` and just
create a `datetime.date` object.
What, re-implement 'strptime' in every program that needs it? And then
debug the result every time?

Even if one doesn't have Python 2.5 or above, surely getting the
string parsed into int values by the standard 'time.strptime' is
better than re-implementing it every time.
Maybe you should read the docs next time. ;-)
Back at you.

--
\ "If you do not trust the source do not use this program." |
`\ —Microsoft Vista security dialogue |
_o__) |
Ben Finney
Oct 16 '07 #4
On Tue, 16 Oct 2007 18:10:54 +1000, Ben Finney wrote:
Marc 'BlackJack' Rintsch <bj****@gmx.netwrites:
>On Tue, 16 Oct 2007 12:33:33 +0530, krishnakant Mane wrote:
firstly, I can't get a way to convert a string like "1/2/2005" in
a genuan date object which is needed for calculation.

Why? Split the string up, convert the parts to `int` and just
create a `datetime.date` object.

What, re-implement 'strptime' in every program that needs it? And then
debug the result every time?
Yes. Seems easier to me. :-)
Even if one doesn't have Python 2.5 or above, surely getting the
string parsed into int values by the standard 'time.strptime' is
better than re-implementing it every time.
>Maybe you should read the docs next time. ;-)

Back at you.
Got me. I didn't know that `datetime` has a `strptime` now. I just
looked at `date`.

Ciao,
Marc 'BlackJack' Rintsch
Oct 16 '07 #5
hello,
thanks all of you for providing valuable help.
right now I am confused about the delta object.
how can I extract the difference between two dates in terms of day
using the delta object?
I tried reading the python docs but did not understand the concept of
delta object and how can I measure the difference in terms of days
between two dates.
I expect that the days would be integers.
secondly the format of my date is actually "16/10/2007", and this is
all in varchar field inside a postgresql database.
I understand that datetime.datetime.strptime would convert this string
"16/10/2007" into a date object which I can then compare with the
current date created by datetime.now().
is that right?
if yes then please explain me how I can get the delta object to give
me results in days.
regards,
Krishnakant.
Oct 16 '07 #6
# Example

import datetime
def days_old(birth_year=1974,birth_month=12,birth_day= 7):
return (datetime.date.today() -
datetime.date(birth_year,birth_month,birth_day) ).days
>>days_old()
12000

krishnakant Mane wrote:
hello,
thanks all of you for providing valuable help.
right now I am confused about the delta object.
how can I extract the difference between two dates in terms of day
using the delta object?
I tried reading the python docs but did not understand the concept of
delta object and how can I measure the difference in terms of days
between two dates.
I expect that the days would be integers.
secondly the format of my date is actually "16/10/2007", and this is
all in varchar field inside a postgresql database.
I understand that datetime.datetime.strptime would convert this string
"16/10/2007" into a date object which I can then compare with the
current date created by datetime.now().
is that right?
if yes then please explain me how I can get the delta object to give
me results in days.
regards,
Krishnakant.
--
Shane Geiger
IT Director
National Council on Economic Education
sg*****@ncee.net | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
Oct 16 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ralph Freshour | last post by:
What's a good way to calculate the number of days between two dates in the following format: 2003-07-15 2003-08-02 I've looked at the PHP date functions but I'm still a bit lost...
4
by: Hans Gruber | last post by:
Hi all, I have been struggling with a problem all day, I have been unable to come up with a working solution. I want to write a function which takes 2 unix timestamps and calculates the...
10
by: riki | last post by:
hello, i need to calculate num of days between 2 dates... i get separate parts of dates from html form, then i need to "make" begining and ending date and calculate difference between them......
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
5
by: Carlos | last post by:
Hi all, I have used the datetime class, but I just came up with a situation that need to have some advise. If I now how much time have passed, can I know the original date?. Lets say that I know...
2
by: Scott | last post by:
Hi All I have an application that has a start date and a finish date I need to calculate the number of days from the start date to the end date and then display this in a read only text box... ...
4
by: Yotam | last post by:
Hi, I need some help with JS. I will be grateful, if you can help me out. I have two date fields (check in, check out) and "number of days" field. I want the script to calculate automatically...
5
by: Julius | last post by:
Hej dudes, I need to calc the difference between two timestamps / dates ... For example what i need to calculate: Date 1: 2007.11.06 - 20:13:04 Date 2: 2007.11.07 - 21:13:04 Difference:...
3
by: John | last post by:
Hi How can I calculate days from the two dates entered in date time picker fields on winform by a user? Thanks Regards
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.