Operating System - HP-UX
1828621 Members
1325 Online
109983 Solutions
New Discussion

UNIX date formatting feature - printf style

 
SOLVED
Go to solution
Danny Fang
Frequent Advisor

UNIX date formatting feature - printf style

Hi,

I’m attempting to use the ‘date’ command with the C-style formatting to format my variables:

Prefix=20
Year=06
Month=MM
Day=DD

I’d like to format it using the C language style format i.e.


printf("%02d%02d%02d%02d" $prefix $year $month $day)

and also in the following format:
printf("%02d%02d-%02d-%02d" $prefix $year $month $day)

I’ve tried using the “- - date “ option:
mz[tmp]$ date --date="20060918"
Mon Sep 18 00:00:00 SGT 2006

mz[tmp]$ date --date="2006-09-18"
Mon Sep 18 00:00:00 SGT 2006

Could anyone show me how I can set the date formatting (specifying the number of digit formats), using ‘date’ utility, or even any other utilities?

Thanks
Danny
3 REPLIES 3
uadm26
Super Advisor
Solution

Re: UNIX date formatting feature - printf style

Hi, Danny

today=`date +%Y-%m-%d`

Good Luck,
Joel
Peter Godron
Honored Contributor

Re: UNIX date formatting feature - printf style

Danny,
can you please clarify:
Are you trying to update the date display to a certain format ( Display Formatted Date Using Local Language Conversion )
or
are you trying to display the date in a certain format ( date "+DATE: %m/%d/%y%nTIME: %H:%M:%S" )

See man for both
Peter Nikitka
Honored Contributor

Re: UNIX date formatting feature - printf style

Hi,

it seems to me, that you are using GNU-date. Using the option '--date' does not have any influence of the output style, it just changes the input date+time. Use the well-known %-modifier (see 'man stftime') to change the output format:

date --date="" '+%Y%m%d'

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"