Sponsored Content
Top Forums Shell Programming and Scripting Getting a Date value based on day Post 303015845 by Yoda on Friday 13th of April 2018 10:15:47 PM
Old 04-13-2018
If you have python installed, here is an approach:-
Code:
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 = datetime.date(int(file[10:-4]), int(file[14:-2]), int(file[16:]))

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

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

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
STRFTIME(3)						   BSD Library Functions Manual 					       STRFTIME(3)

NAME
strftime -- format date and time LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <time.h> size_t strftime(char * restrict buf, size_t maxsize, const char * restrict format, const struct tm * restrict timeptr); size_t strftime_l(char *restrict buf, size_t maxsize, const char * restrict format, const struct tm *restrict timeptr, locale_t loc); DESCRIPTION
The strftime() function formats the information from timeptr into the buffer buf according to the string pointed to by format. The function strftime_l() does the same as strftime() but takes an explicit locale rather than using the current locale. The format string consists of zero or more conversion specifications and ordinary characters. All ordinary characters are copied directly into the buffer. A conversion specification consists of a percent sign ``'%''' and one other character. No more than maxsize characters will be placed into the array. If the total number of resulting characters, including the terminating NUL character, is not more than maxsize, strftime() returns the number of characters in the array, not counting the terminating NUL. Otherwise, zero is returned and the buffer contents are indeterminate. The conversion specifications are copied to the buffer after expansion as follows:- %A is replaced by national representation of the full weekday name. %a is replaced by national representation of the abbreviated weekday name. %B is replaced by national representation of the full month name. %b is replaced by national representation of the abbreviated month name. %C is replaced by (year / 100) as decimal number; single digits are preceded by a zero. %c is replaced by national representation of time and date. %D is equivalent to ``%m/%d/%y''. %d is replaced by the day of the month as a decimal number (01-31). %E* %O* POSIX locale extensions. The sequences %Ec %EC %Ex %EX %Ey %EY %Od %Oe %OH %OI %Om %OM %OS %Ou %OU %OV %Ow %OW %Oy are supposed to provide alternate representations. Additionally %OB implemented to represent alternative months names (used standalone, without day mentioned). %e is replaced by the day of the month as a decimal number (1-31); single digits are preceded by a blank. %F is equivalent to ``%Y-%m-%d''. %G is replaced by a year as a decimal number with century. This year is the one that contains the greater part of the week (Monday as the first day of the week). %g is replaced by the same year as in ``%G'', but as a decimal number without century (00-99). %H is replaced by the hour (24-hour clock) as a decimal number (00-23). %h the same as %b. %I is replaced by the hour (12-hour clock) as a decimal number (01-12). %j is replaced by the day of the year as a decimal number (001-366). %k is replaced by the hour (24-hour clock) as a decimal number (0-23); single digits are preceded by a blank. %l is replaced by the hour (12-hour clock) as a decimal number (1-12); single digits are preceded by a blank. %M is replaced by the minute as a decimal number (00-59). %m is replaced by the month as a decimal number (01-12). %n is replaced by a newline. %O* the same as %E*. %p is replaced by national representation of either "ante meridiem" (a.m.) or "post meridiem" (p.m.) as appropriate. %R is equivalent to ``%H:%M''. %r is equivalent to ``%I:%M:%S %p''. %S is replaced by the second as a decimal number (00-60). %s is replaced by the number of seconds since the Epoch, UTC (see mktime(3)). %T is equivalent to ``%H:%M:%S''. %t is replaced by a tab. %U is replaced by the week number of the year (Sunday as the first day of the week) as a decimal number (00-53). %u is replaced by the weekday (Monday as the first day of the week) as a decimal number (1-7). %V is replaced by the week number of the year (Monday as the first day of the week) as a decimal number (01-53). If the week containing January 1 has four or more days in the new year, then it is week 1; otherwise it is the last week of the previous year, and the next week is week 1. %v is equivalent to ``%e-%b-%Y''. %W is replaced by the week number of the year (Monday as the first day of the week) as a decimal number (00-53). %w is replaced by the weekday (Sunday as the first day of the week) as a decimal number (0-6). %X is replaced by national representation of the time. %x is replaced by national representation of the date. %Y is replaced by the year with century as a decimal number. %y is replaced by the year without century as a decimal number (00-99). %Z is replaced by the time zone name. %z is replaced by the time zone offset from UTC; a leading plus sign stands for east of UTC, a minus sign for west of UTC, hours and min- utes follow with two digits each and no delimiter between them (common form for RFC 822 date headers). %+ is replaced by national representation of the date and time (the format is similar to that produced by date(1)). %-* GNU libc extension. Do not do any padding when performing numerical outputs. %_* GNU libc extension. Explicitly specify space for padding. %0* GNU libc extension. Explicitly specify zero for padding. %% is replaced by '%'. SEE ALSO
date(1), printf(1), ctime(3), printf(3), strptime(3), wcsftime(3) STANDARDS
The strftime() function conforms to ISO/IEC 9899:1990 (``ISO C90'') with a lot of extensions including '%C', '%D', '%E*', '%e', '%G', '%g', '%h', '%k', '%l', '%n', '%O*', '%R', '%r', '%s', '%T', '%t', '%u', '%V', '%z', '%+'. The peculiar week number and year in the replacements of '%G', '%g' and '%V' are defined in ISO 8601: 1988. The strftime_l() function con- forms to IEEE Std 1003.1-2008 (``POSIX.1''). BUGS
There is no conversion specification for the phase of the moon. The strftime() function does not correctly handle multibyte characters in the format argument. BSD
June 25, 2012 BSD
All times are GMT -4. The time now is 02:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy