venkat
venkat

Reputation: 77

converting month number to date using KornShell

I am trying to convert month number to name, but it is giving output as current month instead of the date given in the variable.

KornShell (ksh) Code:

datep= 2013-10-22
echo $datep |printf "%(%B)T\n"

Upvotes: 0

Views: 1658

Answers (2)

chepner
chepner

Reputation: 531948

printf doesn't read from standard input, so it is assuming today's date as the default argument for the %T format; you need to provide the date as an argument instead.

printf "%(%B)T\n" "$datep"

Upvotes: 1

fedorqui
fedorqui

Reputation: 290135

Do it like this:

$ datep="2013-10-22"
$ date -d"$datep" "+%B"
October

As per man date,

-d, --date=STRING

display time described by STRING, not 'now'

So we get:

$ date -d"$datep"
Tue Oct 22 00:00:00 CEST 2013

Then you say you want the %B, that is, also from man date:

%B

locale's full month name (e.g., January)

So it is just a matter of using the format at the end of the string.

Other examples:

$ date -d"$datep" "+%Y" #year
2013
$ date -d"$datep" "+%F" #date
2013-10-22
$ date -d"$datep" "+%T" #time (if not given, gets the default)
00:00:00

Upvotes: 0

Related Questions