How to display the date and time in the Linux Terminal

Configurare noua (How To)

Situatie

The date command is found in the Bash shell, which is the default shell in most Linux distributions and even macOS.

Solutie

Run the date command to see this information. It prints the current date and time for your timezone:

date

The default formatting looks a little goofy. Why isn’t the year printed after the month and day, instead of being tagged on at the end, behind the timezone? Have no fear: If it’s control over the format of the output you want, date delivers it in spades. There are more than 40 options you can pass to date to instruct it to format its output precisely as you’d like.

To use any of the options type date, a space, a plus sign +, and the option including the leading percentage sign. The %c (data and time in locale format) option causes the date and time to be printed in the normalized format associated with your locale. Your locale is set by the geographical and cultural information you provided when you installed your operating system. The locale governs such things as the currency symbol, paper sizes, timezone, and other cultural norms.

date +%c

The year now appears in a more natural position in the output. You can pass several options to date at once. A sequence of options is called a format string. To see the name of the day (%A), the day of the month (%d) and the month name (%B), use this command:

date +%A%d%B

That worked, but it is ugly. No problem, we can include spaces as long as we wrap the entire format string in quotation marks. Note that the + goes outside the quotation marks.

date +”%A %d %B”

You can add text to the format string, like this:

date +”Today is: %A %d %B”

Options to Display the Date and Time

%c: Prints the date and time in the format for your locale, including the timezone.

Options to Display the Date

%D: Prints the date in mm/dd/yy format.

%F: Prints the date in yyyy-mm-dd format.

%x: Prints the date in the format for your locale.

Options to Display the Month

%b or %h: Prints the name of the month abbreviated to Jan, Feb, Mar, etc.

%B: prints the full name of the month, January, February, March, etc.

%m: Prints the number of the month, with a leading zero if required 01, 02, 03 … 12.

Tip solutie

Permanent

Voteaza

(4 din 6 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?