#1 2023-03-17 23:51

skates
Member
Registered: 2013-03-01
Posts: 12

Pascal Increment by Month

I've been using the following regex to format dates on monthly statements.

Expression: ([\d]{4})-([\d]{2})-([\d]{2}) Replace: $1 $2-$3

I now have to increment the month by one (1) and change the day from the 15th to the 1st.

I'm trying to piece together bits of code to accomplish this in Pascal but I'm not familiar enough with it. This is what I have so far but isn't working (invalid number of parameters).

var
  myDate : TDateTime;
begin
  myDate := StrToDate('yyyy mm-01');
  IncMonth(myDate);
  ShowMessage('myDate + 1 month = ' +DateToStr(myDate));
end.

Any help is appreciated.

Last edited by skates (2023-03-18 16:52)


Happy ReNamer User  :)

Offline

#2 2023-03-19 09:45

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,367

Re: Pascal Increment by Month

The script below will find all dates in format "YYYY-MM-DD", add 1 month, set the day component to 1st, and reformat the date as "YYYY MM-DD".

var
  Positions: TIntegerArray;
  Matches: TWideStringArray;
  I, CountMatches: Integer;
  DateStr: String;
  Date: TDateTime;
  Year, Month, Day: Word;
begin
  CountMatches := FindRegEx(FileName, '\d{4}-\d{2}-\d{2}', False, Positions, Matches);
  for I := CountMatches - 1 downto 0 do
  begin
    if TryScanDateTime('yyyy-mm-dd', Matches[I], Date) then
    begin
      Date := IncMonth(Date, 1);
      DecodeDate(Date, Year, Month, Day);
      Date := EncodeDate(Year, Month, 1);
      DateStr := FormatDateTime('yyyy mm-dd', Date);
      Delete(FileName, Positions[I], Length(Matches[I]));
      Insert(DateStr, FileName, Positions[I]);
    end;
  end;
end.

Function reference: https://www.den4b.com/wiki/ReNamer:Pasc … :Functions

Offline

#3 2023-03-21 19:18

skates
Member
Registered: 2013-03-01
Posts: 12

Re: Pascal Increment by Month

Thanks for this.

However, I didn't get very far due to the following error:

"Preview process has been terminated due to a critical error:

Pascal Script Compile:
[Line 2] Error: Unknown type 'TIntegerArray'
"


Happy ReNamer User  :)

Offline

#4 2023-03-22 12:22

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: Pascal Increment by Month

Which version of ReNamer do you use?

Index» News» ReNamer 7.4

2022-09-24
ReNamer 7.4 has been released.
. . .
Added FindRegEx, . . . . and TIntegerArray type to Pascal Script.
. . .







 


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#5 2023-03-22 21:13

skates
Member
Registered: 2013-03-01
Posts: 12

Re: Pascal Increment by Month

Thanks for this.

It goes to show how well this program has been working for me. I was using 6.9 Pro and the license had expired some time ago, but I had no reason to check for an updated version.

I don't recall there being an expiry date in the 'About' for Version 6. Anyway, I've updated to 7.4 now. I've set an expiry reminder for the next version and will be happy to upgrade again. This forum feedback has never failed to solve my issues.  ReNamer is the most reliable, useful, and affordable software I've ever used.

The script is working fine. Many thanks.


Happy ReNamer User  :)

Offline

Board footer

Powered by FluxBB