Calculating Number of Months and Days between Two Dates

I have two dates. I need to figure out how many days and months are
between the two. What is the best way to do it?
1
Roman
8/29/2006 10:27:41 PM
📁 sybase.powerbuilder.general
📃 62418 articles.
⭐ 17 followers.

💬 9 Replies
👁️‍🗨️ 1901 Views

The number of days can be calculated with the PB function RelativeDate. The 
number of months can be calculated with the PFC function of_RelativeMonth 
(pfc_n_cst_datetime).

Brad

"Roman Kastin" <roman DOT kastin AT gsa.gov> wrote in message 
news:[email protected]
>I have two dates. I need to figure out how many days and months are
> between the two. What is the best way to do it? 


0
Brad
8/30/2006 2:09:12 AM
For months, it's easy:

number of years = Year(Date2) = Year(Date1)
number of months = Month(Date2) - Month(Date1) + (number of years * 12)

For days, it's more difficult.  If the day of Date2 > day of Date1, you can
just use:
number of days = Day(Date2) - Day(Date1)

For example, 6/17/06 as the first date and 8/28/06 as the second date, you
would get
2 months and 11 days.

But, when you have 6/28/06 and 8/17/06, how do you want to figure out the
number of days?  You could do:

(last day of the month of the first date - day of the month of the first
date) + day of the month of the second date

So, in our example you'd get 1 month and 19 days.

if number of days < 0 then
    number of months -= 1
    number of days = (last day of month - Day (Date1)) + Day (Date2)
end

For the last day of month, you would need to take into account which month
and for February account for leap years.

If you wanted to use the n_cst_datetime object in the PFC, you have the
functions of_LastDayOfMonth() and of_MonthsAfter() to do some of the
calculations above.

HTH,

steve
[TeamSybase]

"Roman Kastin" <roman DOT kastin AT gsa.gov> wrote in message 
news:[email protected]
>I have two dates. I need to figure out how many days and months are
> between the two. What is the best way to do it? 


0
Steve
8/30/2006 2:28:33 AM
Both RelativeDate and of_RelativeMonth will give you a new date/month an 
offset number of days/months ahead/behind the current date.  It won't tell 
you how many months/days are between 2 date values.

steve
[TeamSybase]

"Brad Wery [TeamSybase]" <bradweryatagricoreunited.com> wrote in message 
news:[email protected]
> The number of days can be calculated with the PB function RelativeDate. 
> The number of months can be calculated with the PFC function 
> of_RelativeMonth (pfc_n_cst_datetime).
>
> Brad
>
> "Roman Kastin" <roman DOT kastin AT gsa.gov> wrote in message 
> news:[email protected]
>>I have two dates. I need to figure out how many days and months are
>> between the two. What is the best way to do it?
>
> 


0
Steve
8/30/2006 2:32:36 AM
If you are using ASA you can use the date functions of the database also, 
like DATEDIFF.

"Roman Kastin" <roman DOT kastin AT gsa.gov> wrote in message 
news:[email protected]
>I have two dates. I need to figure out how many days and months are
> between the two. What is the best way to do it? 


0
Hans
8/30/2006 7:02:27 AM
I ended up using your suggestion and had to create a global function
to calculate number of days between two dates to be called from the
datawindow. 
You would think that this would be easier.
Thanks for your help!

Roman

On 29 Aug 2006 19:28:33 -0700, "Steve Katz [TeamSybase]"
<[email protected]> wrote:

>For months, it's easy:
>
>number of years = Year(Date2) = Year(Date1)
>number of months = Month(Date2) - Month(Date1) + (number of years * 12)
>
>For days, it's more difficult.  If the day of Date2 > day of Date1, you can
>just use:
>number of days = Day(Date2) - Day(Date1)
>
>For example, 6/17/06 as the first date and 8/28/06 as the second date, you
>would get
>2 months and 11 days.
>
>But, when you have 6/28/06 and 8/17/06, how do you want to figure out the
>number of days?  You could do:
>
>(last day of the month of the first date - day of the month of the first
>date) + day of the month of the second date
>
>So, in our example you'd get 1 month and 19 days.
>
>if number of days < 0 then
>    number of months -= 1
>    number of days = (last day of month - Day (Date1)) + Day (Date2)
>end
>
>For the last day of month, you would need to take into account which month
>and for February account for leap years.
>
>If you wanted to use the n_cst_datetime object in the PFC, you have the
>functions of_LastDayOfMonth() and of_MonthsAfter() to do some of the
>calculations above.
>
>HTH,
>
>steve
>[TeamSybase]
>
>"Roman Kastin" <roman DOT kastin AT gsa.gov> wrote in message 
>news:[email protected]
>>I have two dates. I need to figure out how many days and months are
>> between the two. What is the best way to do it? 
>
0
Roman
8/30/2006 5:07:36 PM
What on earth was I thinking?


"Steve Katz [TeamSybase]" <[email protected]> wrote in message 
news:[email protected]
> Both RelativeDate and of_RelativeMonth will give you a new date/month an 
> offset number of days/months ahead/behind the current date.  It won't tell 
> you how many months/days are between 2 date values.
>
> steve
> [TeamSybase]
>
> "Brad Wery [TeamSybase]" <bradweryatagricoreunited.com> wrote in message 
> news:[email protected]
>> The number of days can be calculated with the PB function RelativeDate. 
>> The number of months can be calculated with the PFC function 
>> of_RelativeMonth (pfc_n_cst_datetime).
>>
>> Brad
>>
>> "Roman Kastin" <roman DOT kastin AT gsa.gov> wrote in message 
>> news:[email protected]
>>>I have two dates. I need to figure out how many days and months are
>>> between the two. What is the best way to do it?
>>
>>
>
> 


0
Brad
8/30/2006 5:52:03 PM
You were probably under the spell of the same evil sorceror that caused me 
to give the reply I did in this thread:

http://groups.google.com/group/sybase.public.powerbuilder.general/browse_frm/thread/4586a0a0b57667c6/7839346c3560729b?lnk=st&q=ken+balakrishnan+powerbuilder+relativedate+daysafter&rnum=1#7839346c3560729b



"Brad Wery [TeamSybase]" <bradweryatagricoreunited.com> wrote in message 
news:[email protected]
> What on earth was I thinking?
>
>
> "Steve Katz [TeamSybase]" <[email protected]> wrote in message 
> news:[email protected]
>> Both RelativeDate and of_RelativeMonth will give you a new date/month an 
>> offset number of days/months ahead/behind the current date.  It won't 
>> tell you how many months/days are between 2 date values.
>>
>> steve
>> [TeamSybase]
>>
>> "Brad Wery [TeamSybase]" <bradweryatagricoreunited.com> wrote in message 
>> news:[email protected]
>>> The number of days can be calculated with the PB function RelativeDate. 
>>> The number of months can be calculated with the PFC function 
>>> of_RelativeMonth (pfc_n_cst_datetime).
>>>
>>> Brad
>>>
>>> "Roman Kastin" <roman DOT kastin AT gsa.gov> wrote in message 
>>> news:[email protected]ax.com...
>>>>I have two dates. I need to figure out how many days and months are
>>>> between the two. What is the best way to do it?
>>>
>>>
>>
>>
>
> 


0
Ken
8/30/2006 9:53:27 PM
LOL. It's good to know I'm not alone...

"Ken Balakrishnan" <[email protected]> wrote in message 
news:[email protected]
> You were probably under the spell of the same evil sorceror that caused me 
> to give the reply I did in this thread:
>
> http://groups.google.com/group/sybase.public.powerbuilder.general/browse_frm/thread/4586a0a0b57667c6/7839346c3560729b?lnk=st&q=ken+balakrishnan+powerbuilder+relativedate+daysafter&rnum=1#7839346c3560729b
>
>
>
> "Brad Wery [TeamSybase]" <bradweryatagricoreunited.com> wrote in message 
> news:[email protected]
>> What on earth was I thinking?
>>
>>
>> "Steve Katz [TeamSybase]" <[email protected]> wrote in message 
>> news:[email protected]
>>> Both RelativeDate and of_RelativeMonth will give you a new date/month an 
>>> offset number of days/months ahead/behind the current date.  It won't 
>>> tell you how many months/days are between 2 date values.
>>>
>>> steve
>>> [TeamSybase]
>>>
>>> "Brad Wery [TeamSybase]" <bradweryatagricoreunited.com> wrote in message 
>>> news:[email protected]
>>>> The number of days can be calculated with the PB function RelativeDate. 
>>>> The number of months can be calculated with the PFC function 
>>>> of_RelativeMonth (pfc_n_cst_datetime).
>>>>
>>>> Brad
>>>>
>>>> "Roman Kastin" <roman DOT kastin AT gsa.gov> wrote in message 
>>>> news:[email protected]
>>>>>I have two dates. I need to figure out how many days and months are
>>>>> between the two. What is the best way to do it?
>>>>
>>>>
>>>
>>>
>>
>>
>
> 


0
Brad
8/30/2006 11:04:47 PM
Earth?  Sounds like you were somewhere else in the galaxy....or universe.

S'ok.....been there, done that, too.

The penalty, btw, is one week without being allowed to sign with 
[TeamSybase] in our name.<g>

steve
[TeamSybase]

"Brad Wery [TeamSybase]" <bradweryatagricoreunited.com> wrote in message 
news:[email protected]
> What on earth was I thinking?
>
>
> "Steve Katz [TeamSybase]" <[email protected]> wrote in message 
> news:[email protected]
>> Both RelativeDate and of_RelativeMonth will give you a new date/month an 
>> offset number of days/months ahead/behind the current date.  It won't 
>> tell you how many months/days are between 2 date values.
>>
>> steve
>> [TeamSybase]
>>
>> "Brad Wery [TeamSybase]" <bradweryatagricoreunited.com> wrote in message 
>> news:[email protected]
>>> The number of days can be calculated with the PB function RelativeDate. 
>>> The number of months can be calculated with the PFC function 
>>> of_RelativeMonth (pfc_n_cst_datetime).
>>>
>>> Brad
>>>
>>> "Roman Kastin" <roman DOT kastin AT gsa.gov> wrote in message 
>>> news:[email protected]
>>>>I have two dates. I need to figure out how many days and months are
>>>> between the two. What is the best way to do it?
>>>
>>>
>>
>>
>
> 


0
Steve
8/31/2006 4:32:30 AM
Reply:
(Thread closed)