jpys has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: The day before a specific date
by tybalt89 (Monsignor) on May 31, 2023 at 15:15 UTC

    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
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: The day before a specific date
by Corion (Patriarch) on May 31, 2023 at 12:35 UTC

    The answer already provided by hippo to your previous question also works for your current question. How did it fail for you?

    Update: Fixed link to original answer.

      Hello, When I use the exact formula as shown in the first example, it does function but I get dates from 1970. It is unclear to me how to use the string in my script within your example so that I get the results I am looking for. I tried a couple of different ideas, but all have failed me. Clearly I am not understanding what to do or how to make it work in my script. Please bear in mind that at best I am a novice or un-ranked amateur at best, so what seems obvious to you all is surely not clear to me. If it helps you help me, the string in the script that provides the date is $C_dsrd. I cannot figure out how to install it in your process and come up with the correct result. I appreciate your experience, skills and patience.

        A good start would be to post the code you have, and examples of the data you want to provide to it. This helps us see much better where you are.

        Ideally you also show the output you get from the code and also the output you expect.

        An example structure could be:

        #!perl use 5.020; use feature 'signatures'; no warnings 'experimental::signatures'; sub day_before( $day ) { # ... } for my $date (@ARGV) { say sprintf "The day before %s is %s", $date, day_before($day); }