2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Macのdateコマンド(フォーマット指定でエラー)

Posted at

こういうエラーがでました。

$ date -j -f "%b %d %Y" "Jun 26 2018" "+%Y/%m/%d"
Failed conversion of ``Jun 26 2018'' using format ``%b %d %Y''
date: illegal time format
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

調べてみと、どうやら日本語環境での%bに違いがあるようです。

月のショートバージョン

$ date +%b
 6
$ LANG=c date +%b
Jun

日本語環境だと半角スペース+数字

月のロングバージョン

$ date +%B
6月
$ LANG=c date +%B
June

日本語環境だと数字+月

結論

下記は同じ結果です。

LANG=c date -j -f "%b %d %Y" "Jun 26 2018" "+%Y/%m/%d"
LANG=c date -j -f "%b %d %Y" "June 26 2018" "+%Y/%m/%d"
LANG=c date -j -f "%B %d %Y" "Jun 26 2018" "+%Y/%m/%d"
LANG=c date -j -f "%B %d %Y" "June 26 2018" "+%Y/%m/%d"

date -j -f "%b %d %Y" " 6 26 2018" "+%Y/%m/%d"
date -j -f "%b %d %Y" "6月 26 2018" "+%Y/%m/%d"
date -j -f "%B %d %Y" " 6 26 2018" "+%Y/%m/%d"
date -j -f "%B %d %Y" "6月 26 2018" "+%Y/%m/%d"

jオプション指定で%b%Bはどちらでも良さそうです。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?