What's the quickest way to convert a date in one format, say
2008-06-01
to a date in another format, say
Sun 1st June 2008
The important bit is actually the 'Sun' because depending on the dayname, I may need to fiddle other things around - in a non-deterministic fashion. I'm running GNU bash, version 3.2.17(1)-release (i386-apple-darwin9.0).
[Background: The reason that I want to do it from the command line, is that what I really want is to write it into a TextMate command... It's an annoying task I have to do all the time in textMate.]
doesn't seem to cut it, as I get a usage error:
I'm running GNU bash, version 3.2.17(1)-release (i386-apple-darwin9.0), and as far as the man goes, date -d is just for
If you're just looking to get the day of the week, don't try to match strings. That breaks when the locale changes. The
%u
format give you the day number:And indeed, that was a Thursday. You might use that number to index into an array you have in your program, or just use the number itself.
See the date and strftime man pages for more details. The date manpage on OS X is the wrong one, though, since it doesn't list these options that work.
Reading the date(1) manpage would have revealed:
See
man date
for other format options.This option is available on Linux, but not on Darwin. In Darwin, you can use the following syntax instead:
The -f argument specifies the input format and the + argument specifies the output format.
As pointed out by another poster below, you would be wise to use %u (numeric day of week) rather than %a to avoid localization issues.
If you want more control over formatting, you can also add it like this:
to just get the Sun part that you say you want.
Thanks for that sgm. So just so I can come back to refer to it -
Thanks.