I want to change the output format of `date`.

All Gurus once were Newbies
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Please stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions prefer the other forums within the support section.
Before you post please read how to get help
Post Reply
boracay
Level 1
Level 1
Posts: 15
Joined: Fri Jul 13, 2012 7:44 am

I want to change the output format of `date`.

Post by boracay » Mon Aug 20, 2018 6:16 pm

Currently it says something like

Tue Aug 21 06:14:00 +08 2018

The time here is right now 6:14AM. The "+08" part may confuse me later on, years from now, and may make me think the time I wrote the entry was 14:14 (2:14PM), because 6 plus 8 equals 14. Can it write the time zone I'm in?
Last edited by boracay on Mon Aug 20, 2018 6:44 pm, edited 1 time in total.

gm10
Level 12
Level 12
Posts: 4180
Joined: Thu Jun 21, 2018 5:11 pm

Re: I want to change the output format of `date`.

Post by gm10 » Mon Aug 20, 2018 6:39 pm

Guess it doesn't recognize your time zone, for me it puts the time zone identifier there.

Anyway, you can format the output freely, see man date. For example date +\%Y-\%m-\%d_\%H:\%M:\%S.

User avatar
GS3
Level 4
Level 4
Posts: 424
Joined: Fri Jan 06, 2017 7:51 am

Re: I want to change the output format of `date`.

Post by GS3 » Tue Aug 21, 2018 5:24 am

Maybe I am missing something or not understanding the question. The "+8" is the "error" or time difference between the time quoted and Universal Time (UT). It is the time zone. Without that info you do not know to what time zone the timestamp refers to and it becomes meaningless.

Time zone "+8" means the time stamp is 8 hours ahead of UT (Philippines) and you need to subtract that amount of hours to get UT.

I see an email that says
Date: Thu, 16 Aug 2018 10:16:55 -0400
This means the date is the same as
Date: Thu, 16 Aug 2018 14:16:55 0000
which is UT.

As computers communicate over different time zones it is essential to note the time zone of the timestamp or else it becomes meaningless. Even the same computer can change time zone and most do twice a year.

The only "real" time used for time stamps is UT although it may be expressed as a different local time plus the time difference.
HP Compaq Elite 8300 CMT - Linux Mint 18.2 Sonya - Kernel 4.4.0-138-generic X64 - Cinnamon 3.4.4 - Nemo

User avatar
pepperminty
Level 5
Level 5
Posts: 739
Joined: Thu Jun 23, 2011 10:51 pm

Re: I want to change the output format of `date`.

Post by pepperminty » Tue Aug 21, 2018 4:51 pm

I'd prefer to see the ”+08” in parentheses, and include something like ”Philippines”in the output.

Mute Ant
Level 13
Level 13
Posts: 4926
Joined: Tue Sep 03, 2013 7:45 pm
Location: Norfolk UK

Re: I want to change the output format of `date`.

Post by Mute Ant » Tue Aug 21, 2018 5:20 pm

You probably can't change the distribution version of the function. Everyone else expects it to work the same as last year... they <<rely>> on it working the same as last year. This is especially true for commands in /bin and /sbin which are used during boot.

You can parse the normal output into your own format, sometimes called screen-scraping. Your bash script 'date' calls the real binary 'date' like this...

Code: Select all

#!/bin/bash
#
### file:///usr/local/bin/date
#
### Wrapper script to modify the date() function output to show <<just>> the date ... 2018 Aug 18
#
raw="$(/bin/date)"
new="${raw: -4: 4} ${raw: 4: 6}"
echo $new
exit $?
###EOF###

rene
Level 8
Level 8
Posts: 2228
Joined: Sun Mar 27, 2016 6:58 pm

Re: I want to change the output format of `date`.

Post by rene » Tue Aug 21, 2018 6:40 pm

Note also that date's default display format is determined by your locale settings, specifically LC_TIME. If you paste back the output of locale we can try to reproduce getting your format: I've not been able to get it to display without a (textual) time zone identifier even when trying a few UTC+8 choices such as zh_CN.UTF-8 (China), id_ID.UTF-8 (Indonesia) or ph_PH.UTF-8 (Philipines).

Post Reply

Return to “Newbie Questions”