Visit Our UNIX and Linux User Community

Getting a Date value based on day

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting a Date value based on day
# 1  
Old 04-13-2018
Getting a Date value based on day


I have a scenario like this.

I get a file on anyday of the week, so the file name is like this.

I want to get the tuesday date before this day.

For example
FILE_NAME_20180413 I should get the date as 20180410
FILE_NAME_20180404 I should get the date as 20180403

Ho could I do that?


Last edited by Don Cragun; 04-13-2018 at 03:22 PM.. Reason: Add missing CODE and ICODE tags.
# 2  
Old 04-13-2018
What operating system are you using (including release number)?

What shell are you using (including version number)?

If you have a filename like FILE_NAME_20180410, what date do you want? 20180410 or 20180403?

What have you tried to solve this problem on your own?
# 3  
Old 04-13-2018

I am using Korn shell AIX ver 7.1

I tried this , but its giving me an error : date: illegal option -- d

date -d "$d -$(date -d $d +2) days" +"%m-%d-%Y"

If FILE_NAME_20180410 then I would need 20180410.

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code segments.

Last edited by Don Cragun; 04-13-2018 at 10:21 PM.. Reason: Add missing CODE & ICODE tags, again.
# 4  
Old 04-13-2018
If you have python installed, here is an approach:-
import datetime

def previous_weekday(day, weekday):
        days_behind = weekday - day.weekday()
        if days_behind >= 0:
                days_behind -= 7
        return day + datetime.timedelta(days_behind)

files = ['FILE_NAME_20180413','FILE_NAME_20180404','FILE_NAME_20180410']

for file in files:
        day =[10:-4]), int(file[14:-2]), int(file[16:]))

        # 0 - Monday, 1 - Tuesday ...
        if day.weekday() == 1:
                print(file, day.strftime("%Y%m%d"))
                prev_tuesday = previous_weekday(day, 1)
                print(file, prev_tuesday.strftime("%Y%m%d"))

Produces output:-
('FILE_NAME_20180413', '20180410')
('FILE_NAME_20180404', '20180403')
('FILE_NAME_20180410', '20180410')

# 5  
Old 04-13-2018
I asked what version of shell you're using, but you didn't provide an answer to that question. With a recent Korn shell, such as version 93u+, (which may be installed as ksh93 on AIX 7.1) you could try a simplified version of:
for file in "$@"
do	fd=${file##*_}
	printf 'File: %s: %(%a(%w), %Y-%m-%d)T, Previous Tuesday: %(%Y%m%d\n)T' \
	    "$file" "$fd" "$fd $((($(printf '%(%u)T' "$fd")+5)%7)) days ago"

which if invoked with the operands FILE_NAME_20180410, FILE_NAME_20180413, and FILE_NAME_20180409 produces the output:
File: FILE_NAME_20180410: Tue(2), 2018-04-10, Previous Tuesday: 20180410
File: FILE_NAME_20180413: Fri(5), 2018-04-13, Previous Tuesday: 20180410
File: FILE_NAME_20180409: Mon(1), 2018-04-09, Previous Tuesday: 20180403

Last edited by Don Cragun; 04-14-2018 at 12:02 AM.. Reason: Fix typo: doubled \n in printf format.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 04-19-2018
Originally Posted by dnat
I get a file on anyday of the week, so the file name is like this.

I want to get the tuesday date before this day.
What if the date in the file name falls on a Tuesday itself? (e.g. "FILE_NAME_20180417")
I assume, in that case, you want the same date returned, rather than the previous week's Tuesday.

Using Perl's core module Time::Piece, here is a way to do it:

$ #
$ echo "FILE_NAME_20180413" |
  perl -lne 'BEGIN {use Time::Piece}
             ($x = $_) =~ s/^.*_//;
             $t = Time::Piece->strptime($x, "%Y%m%d");
             $weekday = $t->wday;
             $delta = 3 - $weekday;
             if ($weekday < 3) { $delta -= 7 };
             $t += 24*60*60*$delta;
             print $t->strftime("%Y%m%d");

And some more testing is done here.
$ # Generate some test data
$ perl -le 'BEGIN {use Time::Piece}
            sub get_prev_tuesday {
                ($x = shift) =~ s/^.*_//;
                $t = Time::Piece->strptime($x, "%Y%m%d");
                $weekday = $t->wday;
                $delta = 3 - $weekday;
                if ($weekday < 3) { $delta -= 7 };
                $t += 24*60*60*$delta;
                return $t->strftime("%Y%m%d");
            # Generate a list of filenames for testing
            $start_date = Time::Piece->strptime("20180401", "%Y%m%d");
            foreach $offset (0..29) {
                $date = $start_date + 24*60*60*$offset;
                $file_name = "FILE_NAME_".$date->strftime("%Y%m%d");
                $val = get_prev_tuesday($file_name);
                print $file_name, " => ", $val;
FILE_NAME_20180401 => 20180327
FILE_NAME_20180402 => 20180327
FILE_NAME_20180403 => 20180403
FILE_NAME_20180404 => 20180403
FILE_NAME_20180405 => 20180403
FILE_NAME_20180406 => 20180403
FILE_NAME_20180407 => 20180403
FILE_NAME_20180408 => 20180403
FILE_NAME_20180409 => 20180403
FILE_NAME_20180410 => 20180410
FILE_NAME_20180411 => 20180410
FILE_NAME_20180412 => 20180410
FILE_NAME_20180413 => 20180410
FILE_NAME_20180414 => 20180410
FILE_NAME_20180415 => 20180410
FILE_NAME_20180416 => 20180410
FILE_NAME_20180417 => 20180417
FILE_NAME_20180418 => 20180417
FILE_NAME_20180419 => 20180417
FILE_NAME_20180420 => 20180417
FILE_NAME_20180421 => 20180417
FILE_NAME_20180422 => 20180417
FILE_NAME_20180423 => 20180417
FILE_NAME_20180424 => 20180424
FILE_NAME_20180425 => 20180424
FILE_NAME_20180426 => 20180424
FILE_NAME_20180427 => 20180424
FILE_NAME_20180428 => 20180424
FILE_NAME_20180429 => 20180424
FILE_NAME_20180430 => 20180424

Previous Thread | Next Thread
Test Your Knowledge in Computers #306
Difficulty: Easy
The HTML4 standard was published in 2014.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace date in file every day with current date

I Have text like XXX_20190908.csv.gz need to replace Only date in this format with current date every day Thanks! (1 Reply)
Discussion started by: yamasani1991
1 Replies

2. UNIX for Dummies Questions & Answers

Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds)

Below is the sample logfile: Userids Date Time acb Checkout time: 2013-11-20 17:00 axy Checkout time: 2013-11-22 12:00 der Checkout time: 2013-11-17 17:00 xyz Checkout time: 2013-11-19 16:00 ddd Checkout time: 2013-11-21 16:00 aaa Checkout... (9 Replies)
Discussion started by: asjaiswal
9 Replies

3. AIX

Need to get the next day's date of the user entered date

I need to get the next day's date of the user entered date for example: Enter date (yyyy/mm/yy): 2013/10/08I need to get the next day's date of the user entered date Desired Output: 2013/10/09Though there are ways to achieve this is Linux or Unix environment (date command) ,I need to... (1 Reply)
Discussion started by: rpm120
1 Replies

4. Shell Programming and Scripting

finding the previous day date and creating a file with date

Hi guys, I had a scenario... 1. I had to get the previous days date in yyyymmdd format 2. i had to create a file with Date inthe format yyyymmdd.txt format both are different thanks guys in advance.. (4 Replies)
Discussion started by: apple2685
4 Replies

5. UNIX for Dummies Questions & Answers

Getting date -1 day not using GNU date

It's easy as pie to get the date minus one day on opensolaris: date -d "-1 day" +"%Y%m%d"run this command on our crappy Solaris 10 machines however (which I'm guessing doesn't have GNU date running on it) and you get: date: illegal option -- d date: illegal option -- 1 date: illegal option --... (5 Replies)
Discussion started by: rich@ardz
5 Replies

6. Shell Programming and Scripting

how to obtain date and day of the week from `date` command

Hi, does anybody know how to format `date` command correctly to return the day of the week? Thanks -A I work in ksh.... (1 Reply)
Discussion started by: aoussenko
1 Replies

7. Shell Programming and Scripting

value of variable based on time of the day

i would need some help in setting the value of a variable (TIME_NOW) depending on the time of the day ...e.g. if today's date is 12th April 2009 and if the current time is between midnight and 16:59:59 hrs then the TIME_NOW should be yesterday's date i.e. TIME_NOW=11 else if the current time... (3 Replies)
Discussion started by: zainravi
3 Replies

8. UNIX for Dummies Questions & Answers

date - 1 day

Hi, I have been trying just about every unix command to come up with yesterday's date (today's date - 1). I have seen all of the help on this forum, and none of it seems to work for me here. We are using Sun Solaris 9 Unix. I am using this script to create a .txt file with ftp commands that I will... (2 Replies)
Discussion started by: sfedak
2 Replies

9. UNIX for Dummies Questions & Answers

Add a day to a given date

Hi, I have a date field which is a variable field being passed to the script from outside. I need to know how i can add 1 day to it. I have seen example of date subtraction but while adding each time date reaches 31 or 30 i have to put if else condition. Or for leap year also. Kindly let... (4 Replies)
Discussion started by: pallet
4 Replies

10. Shell Programming and Scripting

Getting day from a date...

Hi, I have a date input in MMDDYYYY format.. I have to give the day (whether that DD is sunday/monday...) Is there any command for it... Or do I have to write a script for that... Thanks in Advance Yeheya (1 Reply)
Discussion started by: yeheyaansari
1 Replies

Featured Tech Videos