in reply to The day before a specific date
Since your "yesterday" question explicitly said DD/MM/YYYY and this question explicitly says MMDDYYYY (by which I think you mean MM/DD/YYYY), I will provide both answers. Pick the one you really want.
#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11152433 use warnings; use Time::Piece; use Time::Seconds; { print "DAY is first, then MONTH. That is: DD/MM/YYYY\n"; @ARGV = '01/05/2023'; # DAY comes first my $t = Time::Piece->strptime("$ARGV[0] 12", "%d/%m/%Y %H"); my $daybefore = ($t - ONE_DAY)->dmy("/"); printf <<END, $ARGV[0], $daybefore; Date as supplied: %s Date minus one day: %s END } { print "MONTH is first, then DAY. That is: MM/DD/YYYY\n"; @ARGV = '05/01/2023'; # MONTH comes first my $t = Time::Piece->strptime("$ARGV[0] 12", "%m/%d/%Y %H"); my $daybefore = ($t - ONE_DAY)->mdy("/"); printf <<END, $ARGV[0], $daybefore; Date as supplied: %s Date minus one day: %s END }
Outputs:
DAY is first, then MONTH. That is: DD/MM/YYYY Date as supplied: 01/05/2023 Date minus one day: 30/04/2023 MONTH is first, then DAY. That is: MM/DD/YYYY Date as supplied: 05/01/2023 Date minus one day: 04/30/2023
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
| A reply falls below the community's threshold of quality. You may see it by logging in. |