October 30, 2025, 05:37:20 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Date suggestions

Started by Brian, May 24, 2011, 12:23:51 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Brian

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

sapero

May 24, 2011, 01:07:06 PM #1 Last Edit: May 24, 2011, 01:12:46 PM by sapero
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

Brian

Well, Sapero, you've done it again!

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

Thanks again,

Brian

DennisL

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.  :-\

Brian

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