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?

6
  • 1
    For me I get same 2015-09-02 in ksh as well. Commented Sep 3, 2015 at 6:40
  • You can also try: date '+%F' -d "yesterday" Commented Sep 3, 2015 at 6:41
  • @insanity, Post output of uname -a. Commented Sep 3, 2015 at 6: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. Commented Sep 3, 2015 at 6:48
  • Concur: The features of the date command are independent of which shell you are using. Commented Sep 3, 2015 at 7:00

1 Answer 1

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 # 
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.