Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date - Month in bash without leading 0 or space?

Tags:

date

bash

Anyone know how to display the date like this?

7-1-2019

I currently have this which adds leading 0 to the month

$(LC_ALL=nn_NO.UTF-8 date +'%-d-%m-%Y')

like this

7-01-2019

I use these in lynx dump commands

like image 906
Chris Avatar asked Jan 06 '19 22:01

Chris


People also ask

How do I remove leading zeros in Unix?

How to remove leading zeros in a variable or a string? The variable x is declared using the typeset command. The -Z option of typeset will strip the leading zeros present when used along with -L option. The expression '^0*' will search for a sequence of 0's in the beginning and delete them.

What is date +% s in bash?

date +%S. Displays seconds [00-59] date +%N. Displays in Nanoseconds.

How do I change the date format in bash?

Bash Date format YYYY-MM-DD To format date in YYYY-MM-DD format, use the command date +%F or printf "%(%F)T\n" $EPOCHSECONDS . The %F option is an alias for %Y-%m-%d . This format is the ISO 8601 format.


1 Answers

You have already removed the padding for the day, why not do the same for the month?

$ date +'%-d-%-m-%Y'
7-1-2019

Here's a list of all padding modifiers from man date:

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

- (hyphen) do not pad the field

_ (underscore) pad with spaces

0 (zero) pad with zeros

^ use upper case if possible

# use opposite case if possible

After any flags comes an optional field width, as a decimal number; then an optional modifier, which is either E to use the locale's alternate representations if available, or O to use the locale's alternate numeric symbols if available.

like image 104
Lev Levitsky Avatar answered Oct 02 '22 14:10

Lev Levitsky