如何将日期从“Oct 01 20:00”转换为“Oct 1 20:00”格式(空格填充)?

use*_*519 3 shell-script date

这里Oct 01必须打印为Oct 1没有 0 的 ie Oct 1。我需要在 Oct 和 1 之间有两个空格,比如。

cha*_*aos 9

使用 GNU date

date -d "Oct 01 20:00" "+%b %_d %k:%M"
Run Code Online (Sandbox Code Playgroud)

在哪里:

  • %b: locale 的缩写月份名称(例如,Jan)
  • %_d: 月中的某一天(空格填充)
  • %k: 小时 (0..23)
  • %M: 分钟 (00..59)

输出:

Oct  1 20:00
Run Code Online (Sandbox Code Playgroud)

  • @StéphaneChazelas 感谢`POSIXLY_CORRECT` 我会看看这个。而不是`%_d`,`%e` 可以在posix 中使用:http://pubs.opengroup.org/onlinepubs/009604599/utilities/date.html#tag_04_33_05_01 (3认同)