Delphi Get previous month and year

Good day

What is the best way of getting the previous month and year.

example current month and year 201001

should be 200912

and 201005 should be 201004
henryreynoldsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

twinsoftCommented:
Hi, this is the easiest way to do it

procedure TForm1.Button1Click(Sender: TObject);
begin
 ShowMessage(DateToStr(IncMonth(now, -1)));
end;

This will get a date with previous month. If you want previous year substract 12 months
0
twinsoftCommented:
Sorry for year use this:

uses DateUtils;

procedure TForm1.Button1Click(Sender: TObject);
begin
 ShowMessage(DateToStr(IncYear(now, -1)));
end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mahdi78Commented:
If you want to use you date format, try this

function GetPreviousDate(IntDate: Integer): string;
var Year, Month: word;
begin
Year := StrToInt(Copy(IntToStr(IntDate), 1, 4));
Month := StrToInt(Copy(IntToStr(intDate), 5, 2));
if Month = 1 then result := IntToStr(Year - 1) + '12'
else result := IntTostr(year) + IntToStr(Month - 1);
end;

procedure TForm1.Button1Click(Sender: TObject);

begin
showmessage(GetPreviousDate(201001)) ;
end;
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

8080_DiverCommented:
It appears that, although you said you wanted the "previous month and year", you actually want the previous month and, if the current month is January, you want December of the previous year.
 In that case, twinsoft's solution is the correct one.
Now, if you want to be able to do something like display the year in one TEdit control and the month in another and then selectively either subtract one year or one month, you can accomplish that with the following code.
 

procedure TForm1.SubtractYearClick(Sender: TObject);
begin
 ShowMessage(DateToStr(IncYear(now, -1)));
end;

procedure TForm1.SubtractMonthClick(Sender: TObject);
begin
 ShowMessage(DateToStr(IncMonth(now, -1)));
end;

Open in new window

0
aikimarkCommented:
@henryreynolds

If I read your question correctly, you have a string that contains yyyymm value and you need to calculate/output the yyyymm value for the prior month.

First, you will need to convert your yyyymm sting into a date, using the first day of the month as a default.  Then pass that date value to a version of the twinsoft routine (from http:#28531199).
IncMonth(converteddatevalue, -1)

Next, you will need to format that prior month's date back into yyyymm format.
0
henryreynoldsAuthor Commented:
Thank you very much
0
aikimarkCommented:
@ henryreynolds

Are you sure you meant to accept that comment and not the one above it?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.