1

I want to get yesterday date in YYYY-MM-DD format.

In bash, I can get it by using date '+%F' -d "1 day ago". It gives the output as 2015-09-02.

How can I get the same result in ksh?

sat
  • 14,589
  • 7
  • 46
  • 65
insanity
  • 1,148
  • 6
  • 16
  • 29
  • 1
    For me I get same `2015-09-02` in `ksh` as well. – anubhava Sep 03 '15 at 06:40
  • You can also try: `date '+%F' -d "yesterday"` – anubhava Sep 03 '15 at 06:41
  • @insanity, Post output of `uname -a`. – sat Sep 03 '15 at 06:45
  • 2
    When you use Korn shell, are you on the same system as when you were using Bash, or on a different system? With Bash on Linux, you have GNU CoreUtils, and `date` comes from that. If you're on a different platform, such as Solaris or perhaps Mac OS X, then you get a different `date` program. Your issue is then "how can I make a non-GNU `date` produce the same answer as GNU `date`?" The simplest answer is probably "Install GNU CoreUtils on this other machine". If you're on the same machine, you'll be using the same `date` program and should not run into this problem at all. – Jonathan Leffler Sep 03 '15 at 06:48
  • Concur: The features of the `date` command are independent of which shell you are using. – tripleee Sep 03 '15 at 07:00
  • Or perhaps http://stackoverflow.com/questions/11855008/get-yesterdays-date-in-solaris – tripleee Sep 03 '15 at 07:03

1 Answers1

4

Ksh's printf supports datetime manipulation:

# echo ${.sh.version}
Version AJM 93u+ 2012-08-01
# printf '%(%Y-%m-%d)T\n' today
2015-09-03
# printf '%(%Y-%m-%d)T\n' yesterday
2015-09-02
# printf '%(%Y-%m-%d)T\n' '5 days ago'
2015-08-29
#
pynexj
  • 19,215
  • 5
  • 38
  • 56
  • 1
    Interesting. There are some limits on its interpetation of such arithmetic; `day before yesterday` isn't recognized. You have to say that differently in GNU `date`, too, but … still, interesting. – Jonathan Leffler Sep 03 '15 at 06:51