Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Date calculations with stock and Gnu versions of 'date' UNIX
The stock OS X date command ships with a useful -v command that allows date calculation. For example, to determine the last day of February, you could use the following:
date -v3m -v1d
That's the third month in the first -v, and then less one day in the second -v. man date gives many examples. On the other hand, the Gnu version of date that's available from MacPorts does not support this option. Playing a bit with the --date option for Gnu's date, I came up with:
date --date="march 1 1 day ago"
This results in the same date calculation as in the stock date function. Gnu's date also includes the option of printing rfc-2822 and rfc-3339 dates. The latter can be used for applications like Google that require xls dates:
date --rfc-3339='ns'
2008-07-23 18:28:00.110568000+05:30
The ns means nano-seconds.

Given the different features of the two programs, it might be useful to keep both on your system, with the Gnu version of date being stored as gdate.
    •    
  • Currently 1.80 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (5 votes cast)
 
[8,717 views]  

Date calculations with stock and Gnu versions of 'date' | 6 comments | Create New Account
Click here to return to the 'Date calculations with stock and Gnu versions of 'date'' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Date calculations with stock and Gnu versions of 'date'
Authored by: Austin-Soft.com on Jul 25, '08 08:22:41AM
Actually, to see the last day of February (not the first day of March), the command would be:
date -v3m -v1d -v-1d


[ Reply to This | # ]
Date calculations with stock and Gnu versions of 'date'
Authored by: S Barman on Jul 25, '08 08:56:23PM
To use the above, if you want to create a shell variable to test whether it is a leap year, you can use:
leap=$((`date -v3m -v1d -v-1d +'%d'`-28))
You can then check the variable $leap. If it is one (1), then it is a leap year! Enjoy. Scott

[ Reply to This | # ]
Date calculations with stock and Gnu versions of 'date'
Authored by: rahulbenegal on Jul 26, '08 09:37:51PM

Yes, you are correct, at the time of writing I had overwritten my stock date program.

@soujuorner, perhaps you have another version of date in your path - the gnu version.

Try the gnu option. Or do "which date". If its in /opt/local/bin, it would be the gnu version. If it's /usr/bin/date, that should be the stock version which supports the -v option.

Do "date --help" to see whether it supports "--date" or "-v".



[ Reply to This | # ]
didn't work...
Authored by: sojourner on Jul 25, '08 10:58:57PM
I got the following message when I tried this:
date: illegal option -- v

[ Reply to This | # ]
Date calculations with stock and Gnu versions of 'date'
Authored by: sojourner on Jul 28, '08 02:07:42AM
hmmm... it's in /bin/date

$ date --help
date: illegal option -- - usage: date [-nu] [-r seconds] [+format] date [[[[[cc]yy]mm]dd]hh]mm[.ss]



[ Reply to This | # ]
Date calculations with stock and Gnu versions of 'date'
Authored by: morespace54 on Jul 28, '08 06:57:29AM

same problem here.
/bin/date on 10.4.11...



[ Reply to This | # ]