Help with Date in UNIX


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with Date in UNIX
# 1  
Old 02-28-2012
Help with Date in UNIX

Hi

I have a requirement where i will need the dates to be set in such a way that the start date should be 5 months before and end date should be 3 months before the current date.

Also the start and end dates we set should be the 1st day of the month.


Example:

Code:
Date            :MM/DD/YYYY
Current date :06/27/2012 
 
START_DATE   :01/01/2012
END_DATE       :03/01/2012

Any help in this regard will be greatly appreciated

Last edited by methyl; 02-28-2012 at 02:04 PM.. Reason: please use code tags
# 2  
Old 02-28-2012
Hi.

Do you have GNU date?

i.e.
Code:
$ date "+%m.01.%y" -d "3 months ago"
11.01.11

$ date "+%m.01.%y" -d "8 months ago"
06.01.11

# 3  
Old 02-28-2012
hi Scott

I believe my system does not have a GNU as the commanda you have suggested does not work as expected

Code:
date "+%m.01.%y" -d "3 months ago"
o/p:02.01.12
 
date "+%m.01.%y" -d "8 months ago"
o/p:02.01.12


Last edited by methyl; 02-28-2012 at 02:05 PM.. Reason: code tags
# 4  
Old 02-28-2012
Hi.

That's not the output I'd expect from a non-GNUish date, but anyhoo.

Here's a quick awk one to get you started:
(B is the number of months in the past)

Code:
$ date '+%m %Y' | awk -v B=3 '{print ($1-B>0)?$1".01."$2:12-B+$1".01."$2-1}'
11.01.2011
$ date '+%m %Y' | awk -v B=8 '{print ($1-B>0)?$1".01."$2:12-B+$1".01."$2-1}'
6.01.2011

(zou can use printf to format the output if the leading zero is always needed)

PS: It is often useful to state up front which OS (incl. version) and shell you are using Smilie
This User Gave Thanks to Scott For This Post:
# 5  
Old 02-29-2012
Hi Scott

My issue still persists this time around its for different reason the code u have provided works fine but the month is not in line with requirement

I get single digit month as below:

6-01-2011

but my requirement is :

06-01-2011

Please suggest me as to how to proceed with this
# 6  
Old 02-29-2012
I already suggested you to use printf.
# 7  
Old 02-29-2012
Code:
#! /usr/bin/perl -w
use strict;

my @x = split/\//, $ARGV[0];

if ($x[0] - 5 < 0) {
    print "Start Date: ", (sprintf "%02d", 12 + ($x[0] - 5)), "/01/", ($x[2] - 1), "\n";
    print "End Date: ", (sprintf "%02d", $x[0]+3), "/01/", $x[2], "\n";
}
elsif ($x[0] + 3 > 12) {
    print "Start Date: ", (sprintf "%02d", $x[0]-5), "/01/", $x[2], "\n";
    print "End Date: ", (sprintf "%02d", ($x[0] + 3) - 12), "/01/", ($x[2] + 1), "\n";    
}
else {
    print "Start Date: ", (sprintf "%02d", $x[0]-5), "/01/", $x[2], "\n";
    print "End Date: ", (sprintf "%02d", $x[0]+3), "/01/", $x[2], "\n";
}

Few runs:
Code:
[root@hostname dir]# ./test.pl 02/29/2012
Start Date: 09/01/2011
End Date: 05/01/2012
[root@hostname dir]# ./test.pl 06/25/2012
Start Date: 01/01/2012
End Date: 09/01/2012
[root@hostname dir]# ./test.pl 11/20/2012
Start Date: 06/01/2012
End Date: 02/01/2013


Last edited by balajesuri; 02-29-2012 at 02:53 AM.. Reason: Refined code
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to replace old date with current date dynamically in multiple files present in a folder

I am trying to work on a script where it is a *(star) delimited file has a multiple lines starts with RTG and 3rd column=TD8 I want to substring the date part and I want to replace with currentdate minus 15 days. Here is an example. iam using AIX server $ cat temp.txt RTG*888*TD8*20180201~... (1 Reply)
Discussion started by: Shankar455
1 Replies

2. HP-UX

awk command in hp UNIX subtract 30 days automatically from current date without date illegal option

current date command runs well awk -v t="$(date +%Y-%m-%d)" -F "'" '$1 < t' myname.dat subtract 30 days fails awk -v t="$(date --date="-30days" +%Y-%m-%d)" -F "'" '$1 < t' myname.dat awk command in hp unix subtract 30 days automatically from current date without date illegal option error... (20 Replies)
Discussion started by: kmarcus
20 Replies

3. Shell Programming and Scripting

Converting String Date into UNIX Date

Hi, I have a string date to my unix script(sun solaris). I wanted to convert it into unix date so that I can use it in a conditional statement. Please see below: MyTest.sh -s 2018-05-09 suppdt=$1 # string date passed via arguement as 2018-04-09 curryr=`date '+%Y'` nextyr=`expr... (2 Replies)
Discussion started by: Saanvi1
2 Replies

4. UNIX for Dummies Questions & Answers

Rename all Files in a UNIX Directory from one date format to another date format

Hi Unix Gurus, I would like to rename several files in a Unix Directory . The filenames can have more than 1 underscore ( _ ) and the last underscore is always followed by a date in the format mmddyyyy. The Extension of the files can be .txt or .pdf or .xls etc and is case insensitive ie... (1 Reply)
Discussion started by: pchegoor
1 Replies

5. Shell Programming and Scripting

UNIX date fuction - how to deduct days from today's date

Hi, One of my Unix scripts needs to look for files coming in on Fridays. This script runs on Mondays. $date +"%y%m%d" will give me today's date. How can I get previous Friday's date.. can I do "today's date minus 3 days" to get Friday's date? If not, then any other way?? Name of the files is... (4 Replies)
Discussion started by: juzz4fun
4 Replies

6. Shell Programming and Scripting

Need to convert the date using UNIX

Hi, how to convert the date from DDMONYYY to DD/MM/YYYY In my file I have a the date as 25OCT2008 but I want the O/P as 25/10/2008. Can someone help me Thanks in advance Regards, MKS (6 Replies)
Discussion started by: mksuneel
6 Replies

7. Shell Programming and Scripting

how to get what date was 28 days ago of the current system date IN UNIX

Hi, Anybody knows how to get what date was 28 days ago of the current system date through UNIX script. Ex : - If today is 28th Mar 2010 then I have to delete the files which arrived on 1st Mar 2010, (15 Replies)
Discussion started by: kandi.reddy
15 Replies

8. Shell Programming and Scripting

Compare date from db2 table to yesterday's Unix system date

I am currently running the following Korn shell script which works fine: #!/usr/bin/ksh count=`db2 -x "select count(*) from schema.tablename"` echo "count" I would like to add a "where" clause to the 2nd line that would allow me to get a record count of all the records from schema.tablename... (9 Replies)
Discussion started by: sasaliasim
9 Replies

9. What is on Your Mind?

unix date getting too large

I have heard that in 2039? that the unix date will be too large for the operating system and unix will no longer be usable. anyone else heard of this or is it anothe y2k scare? thanks:confused: (3 Replies)
Discussion started by: dhlopomo
3 Replies

10. UNIX for Dummies Questions & Answers

Changing Creation Date to a Prespecified Date of a File In Unix

Dear Expert, Is there a command to do that in Unix? In such a way that we don't need to actually "write" or modified the content. -- monkfan (4 Replies)
Discussion started by: monkfan
4 Replies
Login or Register to Ask a Question