How to format the date in KornShell script to DD-M

2019-02-24 10:09发布

How do I format a date in a KornShell (ksh) script to DD-MON-YYYY?

I have tried the following:

date '+%d-%h-%Y'

It returns 04-Nov-2009 I need for the Nov to be NOV (all caps). Can this be done with the date utility?

3条回答
干净又极端
2楼-- · 2019-02-24 10:43

This is what finally worked on unix(solaris).

date '+%d-%h-%Y' | tr [:lower:] [:upper:]

returned: 04-NOV-2009

查看更多
混吃等死
3楼-- · 2019-02-24 10:43

The ^ character forces uppercase in the GNU coreutils date (at least, it does in version 6.9.92.4 of coreutils):

$ date '+%d-%^h-%Y'
04-NOV-2009

Unfortunately, ^ is not POSIX standard for date, so you'll probably have to resort to a second command such as the tr suggested by @martin clayton, if you aren't on a GNU system.

查看更多
Fickle 薄情
4楼-- · 2019-02-24 10:44

You could uppercase it yourself if caret uppercase is not supported in your environment:

date '+%d-%h-%Y' | tr 'a-z' 'A-Z'
查看更多
登录 后发表回答