在 Delphi 4 中从 Now 获取 YYYYMM 字符串

Obtain YYYYMM String from Now in Delphi 4

我想从 Now 过程和 Delphi 中当前过程的前一个月获得字符串 'YYYYMM' 4.

例如:202106(现在)和202105(现在 - 1)

怎么买得起?

您可以使用 DecodeDate 将 TDateTime(如 Now)拆分为年、月和日部分。然后,您可以使用 Format 创建具有 4 位数字年份和月份的字符串,作为 2 位带前导零的数字:

var
    Year, Month, Day: Word;
    Today     : TDateTime;
    Yesterday : TDateTime;
    SToday     : String;
    SYesterday : String;
begin
    Today     := Now;
    Yesterday := Today - 1;

    DecodeDate(Today, Year, Month, Day);
    SToday := Format('%04d%02d', [Year, Month]);

    DecodeDate(Yesterday, Year, Month, Day);
    SYesterday := Format('%04d%02d', [Year, Month]);
end;