92

From git I can get the timestamp:

"2011-10-04 12:58:36 -0600"

but is there any way to show it as:

"2011-10-04 06:58:36"

So all I want is to get rid of the -0600 timezone offset. How can I do this? Thanks.

SandyBr
  • 11,459
  • 10
  • 29
  • 27

8 Answers8

112

If you ask about git log, you can try and select most correct form from:

git log --date={relative,local,default,iso,rfc}

--date=local seems to be the best candidate.

To make this permanent, use git config --global log.date local.

Elijah Lynn
  • 12,272
  • 10
  • 61
  • 91
Lazy Badger
  • 94,711
  • 9
  • 78
  • 110
  • 51
    To make this "permanent", use "git config --global log.date local". – rickumali Dec 24 '11 at 00:57
  • 12
    `local` works. Apparently, a commit includes its own timezone, but `local` translates them all to the local timezone. However, I really want ISO or RFC translated to a single timezone. Anyone know how to do that? – cdunn2001 May 17 '12 at 17:32
  • 2
    Ah! `--date=iso-strict-local` for strict ISO8601. Sweet! – cdunn2001 Oct 08 '15 at 21:52
  • 2
    I don't know why `--date=local` with `iso` format is not working my Ubuntu machines. I am still getting TZ offset. Any help please – user2436428 Jan 21 '16 at 15:35
  • 1
    I added your comment on how to make this permanent, to the answer itself @rickumali. Thanks – Elijah Lynn Jun 16 '18 at 05:42
  • Note that this only seems to be recogniced by the `%cd` placeholder when used with `--pretty=format:`, as documented at https://git-scm.com/docs/git-log#_pretty_formats – Andreas Fester Dec 20 '19 at 07:50
39
git log --date=local

Does the trick.

git config --global log.date local
bond
  • 11,236
  • 7
  • 48
  • 62
  • 1
    When I run: `git log --date=local --pretty=format:"%ai,%an,%ae,%s"`, I still get timezone offset. Doesn't `--data=local` work with **iso** dates? – user2436428 Jan 22 '16 at 10:05
  • @user2436428 if you check [git log documentation](https://git-scm.com/docs/git-log) search for _format:_ you will see you need to use: _**%ad**: author date (format respects --date= option)_ instead of _**%ai**: author date, ISO 8601-like format_ – ptha Jun 17 '16 at 09:33
38
TZ=UTC git log --date=local

in order to get non-local-timezone one-timezone output.

sleblanc
  • 3,821
  • 1
  • 34
  • 42
Christian
  • 381
  • 3
  • 2
  • 1
    `TZ=UTC git log --date=iso-local` to use ISO 8601 – bric3 Oct 04 '16 at 08:50
  • 7
    Or `--date='format-local:%Y%m%dT%H%M%SZ'` for a compact ISO 8601 format (or any other format). – robinst Dec 08 '16 at 05:36
  • i use `date = format-local:%a %d-%b-%Y %H:%M` to give `Fri 05-Oct-2018 19:55`. Here is the list of date formats https://stackoverflow.com/a/34778736/58678 – hIpPy Oct 06 '18 at 06:07
10

Unfortunately, using git log --date=local as explained in previous answers changes the output format.

To keep the format as asked (YYYY-MM-DD HH:mm) I had to use:

git log --date=iso-local

But that only works on git 2.7 or above.

Fabio Marreco
  • 2,186
  • 2
  • 23
  • 24
9

To get the format (YYYY-MM-DD HH:hh), you can use:

git log --date=format:%Y-%m-%d\ %H:%M

Works beautifully with Git Standup too: https://github.com/kamranahmedse/git-standup

Will Wright
  • 309
  • 3
  • 3
6

A full command line answer:

TZ=GMT git show -s --format=%cd --date=iso-local
Kevin English
  • 61
  • 1
  • 1
3

jveerman's post was really helpful:

If you want to display the git date in YYYY-MM-DD HH:MM:SS format:

DATE=$(git log -n 1 --pretty=format:"%ad" --date=iso)
echo "Date: ${DATE::20}"

For log format I was able to add this

[log]
date=format:%Y-%m-%d %H:%M:%S

to my ~/.gitconfig

but getting the same nicely formatted date/time added automatically to my commit messages was an ordeal. I found nothing helpful until I added this to the .git/hooks/prepare-commit-msg file:

DATE=$(git log -n 1 --pretty=format:"%ad" --date=iso)
echo "${DATE::20}" >> $1

If you're mainly using the Desktop app, it's lovely to have the exact time of change shown with the commit listing!

Is there any way to make this global, so I don't have to edit each local repo's prepare-commit-msg file ?

papaJupe
  • 61
  • 1
  • 5
1

If you want to display the git date in YYYY-MM-DD HH:MM:SS format:

DATE=$(git log -n 1 --pretty=format:"%ad" --date=iso)
echo "Date: ${DATE::20}"

jveerman
  • 11
  • 2