Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to execute date command in unix server for yesterday. The commands tried are :

date --date="1 day ago"
date --date="1 days ago"  
date --date="yesterday"
date --date="-1 day"

These command work in a server but the same command does not work in few other servers, where date prints properly the current date. Could anyone suggest what could be the issue with the other servers? I get an error like : illegal option -- date=1 day ago

The server Details: SunOS wupsa02a0014 5.10 Generic_147440-15 sun4u sparc SUNW,SPARC-Enterprise

share|improve this question

migrated from Mar 27 '13 at 8:24

This question came from our site for professional and enthusiast programmers.

Two suggestions: (1) ask on a site where the question isn't off-topic; (2) include uname -a for the servers where this isn't working. – NPE Mar 27 '13 at 7:12
That's because --date="1 day ago" isn't portable. – cnicutar Mar 27 '13 at 7:12
@NPE Thanks for suggestion. Could you please give an example how can I add the uname -a ? – Swagatika Mar 27 '13 at 7:13
He isn't asking you to use uname -a to solve your problem, he wants to know which version(s) you are running on the machines giving you the issue. – Randy Howard Mar 27 '13 at 7:14
SunOS wupsa02a0014 5.10 Generic_147440-15 sun4u sparc SUNW,SPARC-Enterprise – Swagatika Mar 27 '13 at 7:15
up vote 4 down vote accepted

--date is not a conventional date option on many unix systems.

If you have a wide variety of UNIX/linux-like platforms to work with, try to examine the man page for date (1) on each of them (if man pages are installed) and see what date options are common to all of them as a starting point.

You might find date -v (adjust) reasonably portable.

Something like date -v-1d may work. Test it on each system to see if it works on all of them. It displays the current data value adjusted one day in the past.

share|improve this answer
Thanks. Then what are the other ways I can get the date/yesterday ? – Swagatika Mar 27 '13 at 7:14
subtract a day from the current date? :-) Edited above. – Randy Howard Mar 27 '13 at 7:14
If you could provide an example would be helpful. – Swagatika Mar 27 '13 at 7:18
I'm curious if the above works on the SunOS you reference in the comments above. – Randy Howard Mar 27 '13 at 7:23
The above mentioned all the ways works in this server : Linux wnl-svr202b 2.6.32-220.7.1.el6.x86_64 #1 SMP Fri Feb 10 15:22:22 EST 2012 x86_64 x86_64 x86_64 GNU/Linux – Swagatika Mar 27 '13 at 7:27

You can simply subtract 24 hours from the TZ environment variable prior to calling date.

$ TZ=GMT date
Wed Mar 27 09:11:02 GMT 2013
$ TZ=GMT+24 date
Tue Mar 26 09:11:05 GMT 2013

Works on both Solaris and Linux.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .