Date command

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date command
# 1  
Old 11-02-2007
Date command

Hello Forum,

I want to retrieve the old date from "date" command.
For example: It's 02 Nov today and i want 7 days old date say 25 oct
then how can i do that???
As i do
date | awk '{print $3-7}'
It returns -5 (As 2-7)
So can anyone help me to solve this out..... I think by making function, it would be easier but i want to do this by making One liner command as i mentioned.....
Any kind of help and suggession would be appreciated.....

Many Thanks
# 2  
Old 11-02-2007
If you have GNU date, then

date --date "7 days ago"

I think the Perderabo's datecalc can also handles these requests.
# 3  
Old 11-02-2007
Date Command


I couldnot think of a one-liner command as u said.
Please find below a script, which calculates previous date..i.e current date-1

Can you please check, if modifying this script a bit will work in your case

Script below
day=`date +%d%`
month=`date +%m%`
year=`date +%Y`
daynm=`date +%a`

case "$daynm" in
"Mon") day=`expr "$day" - 3`
"Sun") day=`expr "$day" - 2`
*) day=`expr "$day" - 1`
if test $day -le 0
month=`expr "$month" - 1`
case "$month" in
year=`expr "$year" - 1`
m=`expr $day - 1`
day=`cal $month $year | grep . | fmt -1 | tail $m | head -1`

if [[ "${day}" = [0-9] ]]

if [[ "${month}" = [0-9] ]]
echo $Final_Date

Thanks ,
Suresh Sampat
# 4  
Old 11-05-2007
Displaying time relative to "now"

You can print the date if you're looking for "hours earlier/later than right now" by manipulating the TZ variable:

# date
Mon Nov  5 09:47:32 CST 2007

# TZ=$(((6+24*7))) date  
Mon Oct 29 09:47:37  2007

It's a bit cheap Smilie but it works. Note that I needed to start with 6 for my local timezone, and then add hours to go backward (7 days of 24 hours each).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. HP-UX

HP/UX command to pull file name/date based on date

HI, Can anyone tell me how to pull the date and file name separated by a space using the find command or any other command. I want to look through several directories and based on a date timeframe (find -mtime -7), output the file name (without the path) and the date(in format mmddyyyy) to a... (2 Replies)
Discussion started by: lnemitz
2 Replies

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

4. Shell Programming and Scripting

How to get tomorrow,yesterday date from date Command

Hi I want to get tomorrow and yesterday date from date command. My shell is KSH and server is AIX. I tried several options, but unable to do. Please help on this. Regards Rajesh (5 Replies)
Discussion started by: rajeshmepco
5 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. Shell Programming and Scripting

date command

Hi is it possible to give the date command like 24th July 2009 ive tried DATE=`date "+%d%m%Y" echo $DATE that only replies 2009 correctly. not sure how to display the month in full or if unix knows how to do i.e 2nd, 24th, 3rd (1 Reply)
Discussion started by: magnia
1 Replies

7. UNIX for Dummies Questions & Answers

date command

Hi All. I'm using date -a to 'drift' the time forward / backwards. The question is - how do I know when its finished 'drifting' ? On some systems I have another time reference I can use but not always. thanks (1 Reply)
Discussion started by: Mudshark
1 Replies

8. Shell Programming and Scripting

want to get previous date from date command in ksh

I want to get previous date from date command. I am using ksh shell. Exmp: today is 2008.09.04 I want the result : 2008.09.03 Please help. Thanks in advance. (4 Replies)
Discussion started by: rinku
4 Replies

9. UNIX for Dummies Questions & Answers

Date Command

we're using HP-UX I need to change the year. What is the date command? Thanks (2 Replies)
Discussion started by: saldana
2 Replies

10. UNIX for Dummies Questions & Answers

date command

Using the date command how do get yesterday's date?? e.g. date '+%b%e%Y' July 30 2002 I need to get July 29 2002 using the date command. Thanx (p.s. sorry if it's a very obvious question) (6 Replies)
Discussion started by: niamo1
6 Replies
Login or Register to Ask a Question