Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calculating Number of Months and Days between Two Dates

1,939 views
Skip to first unread message

Roman Kastin

unread,
Aug 29, 2006, 6:27:41 PM8/29/06
to
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?

Brad Wery [TeamSybase]

unread,
Aug 29, 2006, 10:09:12 PM8/29/06
to
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:51f9f2p6jcnd6kl6q...@4ax.com...

Steve Katz [TeamSybase]

unread,
Aug 29, 2006, 10:28:33 PM8/29/06
to
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:51f9f2p6jcnd6kl6q...@4ax.com...

Steve Katz [TeamSybase]

unread,
Aug 29, 2006, 10:32:36 PM8/29/06
to
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:44f4f0ad@forums-2-dub...

Hans Groeneveld

unread,
Aug 30, 2006, 3:02:27 AM8/30/06
to
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:51f9f2p6jcnd6kl6q...@4ax.com...

Roman Kastin

unread,
Aug 30, 2006, 1:07:36 PM8/30/06
to
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]"

Brad Wery [TeamSybase]

unread,
Aug 30, 2006, 1:52:03 PM8/30/06
to
What on earth was I thinking?


"Steve Katz [TeamSybase]" <steve...@teamsybase.com> wrote in message
news:44f4f8c4$1@forums-1-dub...

Ken Balakrishnan

unread,
Aug 30, 2006, 5:53:27 PM8/30/06
to
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:44f5d043@forums-1-dub...

Brad Wery [TeamSybase]

unread,
Aug 30, 2006, 7:04:47 PM8/30/06
to
LOL. It's good to know I'm not alone...

"Ken Balakrishnan" <kNOe...@cps92.com> wrote in message
news:44f60638@forums-2-dub...

Steve Katz [TeamSybase]

unread,
Aug 31, 2006, 12:32:30 AM8/31/06
to
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:44f5d043@forums-1-dub...

0 new messages