60

I made an alias of the date command to display date in the following format:

2013.06.14.12.10.02

using this command:

alias date = date +"%Y.%m.%d.%H.%M.%S"

Everything works great, except I want to remove the leading zeroes from the output.

There is no way to make it happen by changing the format. I think it can be done only by piping the output to other commands like sed and awk.

The OS I am running is Ubuntu 12.04.2 LTS.

5
  • There are no trailing zeroes in your example (well, unless you count the 0 in the 10 minutes past the hour, but removing that changes meaning of the timestamp). I take it you mean leading zeroes (zeroes at the beginning of each date component), in which case if you are using a GNU userland @j883376's answer will likely be useful. Otherwise, please specify your environment (userland tools are not the same in all Unix-like OSes, and not even all tools might be available on all such OSes).
    – user
    Jun 14 '13 at 7:25
  • yes, the title was wrong, though the post was correct, sorry for confusion.
    – easl
    Jun 14 '13 at 7:33
  • No worries. As a general rule, though, it is always good to specify your environment. OS X is different from OpenBSD is different from AIX is different from GNU. By specifying your environment, you don't risk getting answers which won't be of any use to you (like, say, answers suggesting using Linux's /proc when you are trying to solve a problem on OS X).
    – user
    Jun 14 '13 at 7:40
  • 3
    Just a remark: Be aware that removing leading zeros makes it harder to sort by those dates, if you ever need to.
    – Dubu
    Jul 4 '14 at 7:18
  • See this post: stackoverflow.com/questions/47880412/… Mar 1 '18 at 23:12
102

As per the GNU date manpage:

   By default, date  pads  numeric  fields  with  zeroes.   The  following
   optional flags may follow '%':

   -      (hyphen) do not pad the field

Therefore you can do

alias date="date '+%Y.%-m.%-d.%-H.%-M.%-S'"

and receive

2013.6.14.3.19.31
1
7

Feels silly, but since this question is tagged with /sed, here is a way to do this with sed, as you had mentioned :)

alias date='date +"%Y.%m.%d.%H.%M.%S" | sed "s/^0*//g; s/\.0*/./g"'
2
  • 1
    At two seconds past noon, date will say 2013.06.14.12.00.02, and sed will output 2013.6.14.12..2.  Note that the 00 minutes field disappears completely, leaving two adjacent dots.  This is probably not what the user wants.  To do this correctly in GNU sed, use sed -E -e 's/^0?//' -e 's/\.0?/./g' (to delete zero or one 0).  Or, to do it portably in GNU or POSIX sed, use sed -e 's/^0//' -e 's/\.0/./g' (to delete one 0, or not; it’s OK if the substitution simply fails when there isn’t a leading 0).  … (Cont’d)
    – Scott
    Sep 6 '20 at 5:46
  • 1
    (Cont’d) … Note that a g option is useless on a substitute that’s anchored with ^ or $, since it cannot happen more than once.
    – Scott
    Sep 6 '20 at 5:46
0

XFCE setting for no 0 in front of the time

%l:%M %p
-2

Using printf gives you some flexibility.

For example:

/usr/bin/date "+%b %-2d %-H" | xargs printf "%s %2d %02d"

1
  • What are you saying?  “You can do lots of formatting with printf; go look up the details yourself.”?  This does not produce the YMDHMS format requested by the question, and, after stripping the leading zero from the hour, it puts it back! So this is not an answer to the question.
    – Scott
    Sep 6 '20 at 5:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.