How to Manipulate Date based on number of days?


 
Thread Tools Search this Thread
Operating Systems Solaris How to Manipulate Date based on number of days?
# 1  
Old 03-02-2012
How to Manipulate Date based on number of days?

I have script where in i issue a command to get back 6 month earlier date.
below command works on all linux distribution.
Code:
#  date +%Y-%m-%d --date="2012-03-31 - 183 days ago"
2011-09-30

Is there any solaris equivalent command to do the same?
# 2  
Old 03-02-2012
FWIW I ran that command and got different output:

Quote:
[root@campusweb session]# date +%Y-%m-%d --date="2012-03-31 - 183 days ago"
2012-09-30
[root@campusweb session]# uname -a ; cat /etc/redhat-release
Linux campusweb.ncat.edu 2.6.18-238.9.1.el5PAE #1 SMP Fri Mar 18 12:50:00 EDT 2011 i686 i686 i386 GNU/Linux
Red Hat Enterprise Linux Server release 5.6 (Tikanga)
If you're able to get this to work with the GNU version of date, the stock "Solaris community" response is "compile it and install the GNU version."

If that's not an option, my understanding of what you're trying to get at is to calculate the date of an arbitrary number of days in the past? Basically there's no way you can do it with Solaris's date command and there's no other stock command (other than cal) that really deals with dates. But fret not this shell function should do something similar to what you're wanting (assuming perl is installed, which it should be):

Quote:
whenWas(){
secondsAgo=$(( $1 * 86400 ))
perl -e "use POSIX qw(strftime);print strftime(\"%m/%d/%Y %H:%M:%S\n\", localtime((time-$secondsAgo)))"
}
That's pretty crude but it should be pretty obvious where you want to take it from there.

- Joel
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Manipulate condition to send mail based on output text in file

Hi All, I have a working script as below. echo "Files loaded with $(cat /var/tmp/script.X1.out)" | mail -s "Files loaded with return code" mailid This script takes the output from script.X1.out file and appends the text "Files loaded with return code" and sends the email. Now what I want... (5 Replies)
Discussion started by: midhun3108
5 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. UNIX for Dummies Questions & Answers

Subtracting number of days from current date

Hi All, I am using KSH shell and need to subtract n number of days from the current date .. kindly suggest ! Thanks in advance!:) (20 Replies)
Discussion started by: dev.devil.1983
20 Replies

4. Shell Programming and Scripting

Subtract 2 date columns in .csv file and get output as number of days

Hi, I have one .csv file. I have 2 date columns present in file, column 2 and column 3. I need to calculate how many days exist between 2 dates. I am trying to subtract date column 2 from date column 3. Eg: my file look likes s.no, Start_date,End_Date 1, 7/29/2012,10/27/2012 2,... (9 Replies)
Discussion started by: Dimple
9 Replies

5. AIX

How to manipulate date format?

Hi, I need to convert the below string in 'yyyymmdd' format. e.g., 24 June 2011 -> 20110624 Please help !! (4 Replies)
Discussion started by: ruby
4 Replies

6. Shell Programming and Scripting

Number of days between the current date and user defined date

I am trying to find out the number of days between the current date and user defined date. I took reference from here for the date2jd() function. Modified the function according to my requirement. But its not working properly. Original code from here is working fine. #!/bin/sh... (1 Reply)
Discussion started by: hiten.r.chauhan
1 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

date for two days or 3 days ago

i need a script that can tell me the date 2 days ago or 3 days ago. please help (7 Replies)
Discussion started by: tomjones
7 Replies

9. Shell Programming and Scripting

Sed command to find, manipulate and replace a number

Hi, Im very new to the world of sed so I'm really not even sure if this is possible. What i need to do is read from a flat file and every time i see this line: VAL=123,456 I need to change 456 to 457 for every occurence of this line in the file. The numbers 123 and 456 are different for... (6 Replies)
Discussion started by: LT_2008
6 Replies

10. Shell Programming and Scripting

How to manipulate date?

Hi can u pls help me out with this as i am relatively new to unix. I need to use 4 different types of dates in my shell script * first day of the last(previous) month * last day of the last(previous) month * first day of the current month * (sysdate-1) yesterday's date for the current... (5 Replies)
Discussion started by: phani
5 Replies
Login or Register to Ask a Question