How bash treats literal date value and retrieve year, month and date?

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How bash treats literal date value and retrieve year, month and date?
# 1  
Old 02-13-2017
How bash treats literal date value and retrieve year, month and date?

Hi,

I am trying to add few (say 3 days) to sysdate using -

Code:
date -d '+ 3 days' +%y%m%d

and it works as expected.

But how to add few (say 3 days) to a literal date value and how bash treats a literal value as a date. Can we say just like in ORACLE TO_DATE that my given literal date value is in this format.

I mean, 12/11/04 I know 12 is yy, 11 is dd and 04 is mm but BASH may think 12 as yy, 11 as mm and 04 as dd.

Regards,
Pointers
# 2  
Old 02-13-2017
Hi.

Depending on your OS, shell, etc. (which you forgot to post), and level of expertise, one or more of hte following may be of interest. I often use dateutils ... cheers, drl

Code:
Date, time arithmetic, math

        1) gnu date

        2) ksh ( version 93, not 88; ksh93 M 93t+ ) printf, e.g:
           printf "%(%Y-%m-%d)T\n" "yesterday"

        3) date.pl (limited arithmetic, but "date.pl -d '- 1 day'" works)
           ( https://www.unix.com/tips-tutorials/
           239167-general-purpose-date-script.html)

        4) dconv et al, e.g. ddiff, dconv, strptime (parsing), etc.
           ( dateutils: http://www.fresse.org/dateutils/ )

        5) tm2tm (, OK in 32-bit; fails to compile in 64-bit)
           ( https://www.unix.com/shell-programming-scripting/
           146216-date-difference-between-freebsd-linux.html#post302463136 )

        6) perl custom formatting (with function POSIX::strftime, "perldoc POSIX")

        7) date-cpan.pl, cpan perl date
           ( http://cpansearch.perl.org/src/CWEST/ppt-0.14/src/date/date.jgross )

        8) For small changes, say, "yesterday" in bash, ksh
           TZ=CST+24 date +%Y%m%d
           csh, tcsh
           setenv TZ CST+24 ; date +%Y%m%d

        9) Check for legal date:
           manstat:validata ( http://oldwww.acm.org/perlman/stat/ )

        10) Check for legal date:
            validate (local perl code)

        11) Shell, ksh, date calculations:
            www.unix.com/unix-for-dummies-questions-and-answers/4870-days-elapsed-between-2-dates.html

# 3  
Old 02-13-2017
This isn't being done by BASH, everything here is being done by date - GNU date specifically. Basic POSIX date doesn't have "+ 3 days" (but in my opinion, should.)


GNU date - and most of the civilized world sincd 1988 - understand dates in the ISO standard YYYY/MM/DD form. This method has pretty clear advantages like dates being trivially sortable. So you could do -d "2016/12/31 + 3 days", etc.

Last edited by Corona688; 02-13-2017 at 12:14 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Not able to fetch previous month first date and last date

I am not able to fetch first date and last date previous month date -d -1month +%Y-%m-%d date -d -1month +%Y-%m-%d I need two format dd-mm-yyy previous month 01-03-2016 previous month 31-03-2016 and also only date 1 to 31 Aprriciate your replay (4 Replies)
Discussion started by: jagu
4 Replies

2. Shell Programming and Scripting

Find week of the year for given date using date command inside awk

Hi all, Need an urgent help on the below scenario. script: awk -F"," 'BEGIN { #some variable assignment} { #some calculation and put values in array} END { year=#getting it from array and assume this will be 2014 month=#getting it from array and this will be 05 date=#... (7 Replies)
Discussion started by: vijaidhas
7 Replies

3. Shell Programming and Scripting

How to find all files other than first two dates & last date per month and year?

how to find all files other than first two dates & last date per month and year Hi All, lets say there are following files in directory -rwxr-xr-x 1 user userg 1596 Mar 19 2012 a.txt -rwxr-xr-x 1 user userg 1596 Mar 19 2012 b.txt -rwxr-xr-x 1 user userg ... (6 Replies)
Discussion started by: Makarand Dodmis
6 Replies

4. Shell Programming and Scripting

How to list files that are not first two files date & last file date for every month-year?

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (2 Replies)
Discussion started by: Makarand Dodmis
2 Replies

5. Shell Programming and Scripting

Help with getting last date of previous month and first date of previous 4th month from current date

I have requirment to get last date of previous month and the first date of previous 4th month: Example: Current date: 20130320 (yyyymmdd) Last date of previous month: 20130228 (yyyymmdd) First date of previous 4th month: 20121101 (yyyymmdd) In my shell --date, -d, -v switches are not... (3 Replies)
Discussion started by: machomaddy
3 Replies

6. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

7. Shell Programming and Scripting

Pass the first date and last date of previous month

Hi All, I need to run a job every month at the beginning of the month which is scheduled through autosys, lets say on 03/01/2010. I need to pass the last month's i.e February's first_date = 02/01/2010 and last_date = 02/28/2010 as variables to a stored procedure. Can somebody please pass... (2 Replies)
Discussion started by: vigdmab
2 Replies

8. Homework & Coursework Questions

Date comparison with 'string date having slashes and time zone' in Bash only

1. The problem statement, all variables and given/known data: I have standard web server log file. It contains different columns (like IP address, request result code, request type etc) including a date column with the format . I have developed a log analysis command line utility that displays... (1 Reply)
Discussion started by: TariqYousaf
1 Replies

9. Shell Programming and Scripting

Get yesterday's date in year-month-day format?

Dear All, Actually, i'm doing some reporting job and i need to pass yesterday's date in Year-Month-Day format(e.g. 2009-06-10) to another program for generating 2009-06-10 report. to get today's date, it's easy to just date '+%Y%m%d' , but no idea how can i get this kind of format for... (2 Replies)
Discussion started by: tiger2000
2 Replies

10. Shell Programming and Scripting

yesterday date month/date

Hi expert, I want to retrieve yesterday su log. How to calculate and assign variable value ( 06/23 ) in myVariable ? #!/bin/sh myVariable=yesterday date in month/date cat /var/adm/sulog | grep $myVariable > file.txt many thanks! (5 Replies)
Discussion started by: skully
5 Replies
Login or Register to Ask a Question