代码之家  ›  专栏  ›  技术社区  ›  Mr Goobri

使用GNU日期转换git日期

  •  1
  • Mr Goobri  · 技术社区  · 11 年前

    我想把数字日期转换成Ymd的格式。这是我的尝试:

    $>>git log -1 --format="%cd" | xargs date Y%m%d
    

    但这会返回此错误:

    date: extra operand ‘Thu’
    Try 'date --help' for more information.
    

    如何转换git日期的想法如下:

    Thu Sep 19 17:03:12 2013 +0100
    

    对于这样的事情:

    20130919
    

    谢谢

    3 回复  |  直到 11 年前
        1
  •  10
  •   devnull    11 年前

    如果你能 git 发出UNIX时间戳。说:

    git log -1 --format="%at" | xargs -I{} date -d @{} +%Y%m%d
    

    来自 documentation :

    %at :作者日期,UNIX时间戳

        2
  •  3
  •   konsolebox    11 年前

    而不是使用 %cd 使用 %ct 其将获得时间戳:

    git log -1 --format="%ct" | xargs -i --  date -d '@{}' '+%Y%m%d'
    
        3
  •  2
  •   tripleee    11 年前

    你想要。。。 | xargs -i date -d {} +%Y%m%d .

    请注意 -d 用于传入当前时间以外的日期的选项(以及 xargs -i 将其置于命令行的中间)和 + 指定日期格式说明符(以及缺少的 % 对于 %Y ).

    等效地,没有 xargs ,

    date -d "$(git log -1 --format="%cd")" +%Y%m%d