Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert git date using GNU date

Tags:

git

date

bash

xargs

I would like to convert the git date into the format of Ymd. Here's my attempt:

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

But this returns this error:

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

Any ideas how to convert git date like this:

Thu Sep 19 17:03:12 2013 +0100

To something like this:

20130919

Thanks.

like image 637
Mr Goobri Avatar asked Feb 26 '26 20:02

Mr Goobri


2 Answers

It'd be easier if you make git emit the UNIX timestamp. Say:

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

From the documentation:

%at: author date, UNIX timestamp

like image 112
devnull Avatar answered Mar 01 '26 17:03

devnull


Instead of using %cd, use %ct which would get the timestamp:

git log -1 --format="%ct" | xargs -i --  date -d '@{}' '+%Y%m%d'
like image 29
konsolebox Avatar answered Mar 01 '26 19:03

konsolebox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!