Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to display a specific date in Unix, using the date command, I can't figure out how to display a date other than today's date.

For example, if I was trying to display July 4, 2011 I use

$ date +"%B %d, %Y"

But I keep getting today's date in that format. How can I change it so that it displays the date I want?

share|improve this question
add comment

2 Answers 2

If you're running Linux:

date +"%B %d, %Y" -d 2011-07-04

From man date:

   -d, --date=STRING
          display time described by STRING, not 'now'
share|improve this answer
1  
The answer largely depends on the particular flavour of UNIX the user32270 is using. For example, the suggested variant will not work on HP-UX or older SunOS, in fact, there's no way to display an arbitrary date by date there. –  Leonid Feb 13 '13 at 5:52
1  
@Leonid - Either way, this information will be in man if it is possible in their environment. –  Chris Down Feb 13 '13 at 6:28
add comment

Generally, date is not the tool for that (though some implementations, like GNU date can do it as a non-standard extension).

If you want to do date manipulation portably, the best you can do is probably to use perl (which is installed on virtually every non-embedded Unix) and its POSIX module for the strftime and mktime functions. There is a plethora of other friendlier date manipulation modules for perl but they would generally not be installed by default, at least not with older versions of perl.

perl -MPOSIX -le 'print strftime "%B %d, %Y", @ARGV' 0 0 0 4 7 "$((2011 - 1900))"
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.