IonicWind Software

IWBasic => General Questions => Topic started by: Brian on May 24, 2011, 12:23:51 PM

Title: Date suggestions
Post by: Brian on May 24, 2011, 12:23:51 PM
Hi,

I have a string holding the date, eg, "24/05/2011"

I want to take exactly 1 year off the date (under certain conditions), and then write it back as a string

Is there a date API call to enable me to do this?

Many thanks,

Brian
Title: Re: Date suggestions
Post by: sapero on May 24, 2011, 01:07:06 PM
Hi Brian, try this one - it assumes that the year is the third element:
declare import,sscanf(...)
declare import,sprintf(...)
sub decrement_year(string date)
int d,m,y
sscanf(date, "%d/%d/%d",&d,&m,&y)
sprintf(date, "%02d/%02d/%d",d,m,y-1)
endsub

' example
string date = "24/05/2011"
print date
decrement_year(date)
print date
Title: Re: Date suggestions
Post by: Brian on May 24, 2011, 02:14:17 PM
Well, Sapero, you've done it again!

Thanks for an example I could actually understand, and which worked first time

Thanks again,

Brian
Title: Re: Date suggestions
Post by: DennisL on May 24, 2011, 08:50:13 PM
Just a heads up here that this will be incorrect for a specific case of in a leap year.

i.e. "29/02/2000" minus 1 year would give "29/02/1999" which doesn't exist!  :o
don't know if you'd want it to give "01/03/1999" or "28/02/1999", but you'd have to code in extra checks in either case.  :-\
Title: Re: Date suggestions
Post by: Brian on May 25, 2011, 01:55:30 AM
Dennis,

I wondered about that. Might have a look at leap year dates past and present,
and make sure that the calculated date doesn't land on one of those. I can always
move it forward or back one day if it does

Thanks for the reminder,

Brian