Date command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date command
# 15  
Old 09-11-2012
Try this..

Code:
$ DATE=`date +'%y%m%d'`
$ echo $DATE
120911

or another one

Code:
$ DATE=$(date +'%y%m%d')
$ echo $DATE
120911

# 16  
Old 09-11-2012
Tried both methods, using brackets does seem to make the date command work however the TZ changes don't apply to it so its just outputs todays date
# 17  
Old 09-11-2012
Hi JayJay02,

Can you try the below command?
Code:
DATE=`TZ=GMT-168 date +%y%m%d`

This yielded me
Code:
sirahu@sirahu-dt037:~$ DATE=`TZ=GMT-168 date +%y%m%d` ;echo $DATE
120912

where
Code:
sirahu@sirahu-dt037:~$ date
Tue Sep 11 17:25:45 IST 2012


Last edited by Franklin52; 09-11-2012 at 09:03 AM.. Reason: Please use code tags for data and code samples
# 18  
Old 09-11-2012
All of the above just give todays date, I need it to be seven days previous
# 19  
Old 09-11-2012
If you have FreeBSD try
Code:
date -v-7d +"%y%m%d"

Adjust the format to what you want.

Last edited by 244an; 09-11-2012 at 11:13 AM..
This User Gave Thanks to 244an For This Post:
# 20  
Old 09-11-2012
Sorry I run Solaris, so the above won't work, thats for the input though Smilie
# 21  
Old 09-11-2012
Ok. When I read through this thread I get a feeling that you perhaps haven't tried all different suggestions exactly as they are typed, or?
Please run this command and post exactly the result/error message:
Code:
date -d "7 days ago"

On my system it doesn't work with the suggestion with TZ=TZ+168..., but it seems to work with:
Code:
TZ=CEST+167 date

BUT the limit seems to be 167 hours, 1 hour less than 7 days. "CEST" is the local timezone for my server, you will see yours if you just do date, the characters before the year.

For me it also works with using seconds for 7 days:
Code:
date -r $(($(date +%s) - 604800))

For all these alternatives you can add the format you want at the end of the line, e.g. +"%Y-%m-%d"

Perhaps you also should read this:
https://www.unix.com/answers-frequent...rithmetic.html

Last edited by 244an; 09-11-2012 at 10:14 PM..
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