banner



How To Set Date In Terminal Mac

Bash shell on Unity desktop concept
Fatmawati Achmad Zaenuri/Shutterstock.com

The date command is found in the Bash beat, which is the default shell in most Linux distributions and even macOS. This tutorial shows you lot how to chiefdate on the command line and how you can utilize it in shell scripts to do more than simply print the fourth dimension.

Run the date command to run across this data. It prints the current engagement and time for your timezone:

date

Output of the date command

The default formatting looks a footling 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 yous want, engagement delivers it in spades. There are more than xl options y'all tin can pass to date to instruct it to format its output precisely every bit you'd like.

To use whatever of the options type date, a infinite, a plus sign +, and the pick including the leading percentage sign. The %c (data and time in locale format) option causes the date and fourth dimension to be printed in the normalized format associated with your locale. Your locale is prepare past the geographical and cultural information you lot provided when y'all installed your operating system. The locale governs such things every bit the currency symbol, newspaper sizes, timezone, and other cultural norms.

engagement +%c

Output of the date command with c option

The twelvemonth now appears in a more natural position in the output.

You can pass several options to date at once. A sequence of options is chosen a format string. To see the proper noun of the twenty-four hour period (%A), the mean solar day of the month (%d) and the month name (%B), use this command:

date +%A%d%B

Output of the date command with A d B options

That worked, only it is ugly. No trouble, nosotros can include spaces as long as nosotros wrap the entire format cord in quotation marks. Note that the + goes outside the quotation marks.

date +"%A %d %B"

Output of the date command with A d B option with spaces

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

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

Output of the data command with user added text

Scrolling up and down through the date man page looking for the choice you want shortly becomes tiresome. We've wrangled the options into groups to assist you find your way around them more easily.

Options to Display the Engagement and Time

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

Output of the date command

Options to Brandish the Appointment

  • %D: Prints the date in mm/dd/yy format.
  • %F: Prints the appointment in yyyy-mm-dd format.
  • %ten: Prints the date in the format for your locale.

Output of the date command with D F x options

Options to Display the Twenty-four hour period

  • %a: Prints the name of the day, abbreviated to Monday, Tue, Midweek, etc.
  • %A: Prints the full name of the day, Monday Tuesday, Wed, etc.
  • %u: Prints the number of the twenty-four hours of the calendar week, where Monday=1, Tuesday=2, Wednesday=three, etc.
  • %w: Prints the number of the day of the calendar week, where Sunday=0, Monday=ane, Tuesday=2, etc.
  • %d: Prints the day of the month, with a leading zippo (01, 02 … 09) if required.
  • %eastward: Prints the day of the calendar month, with a leading space (' 1', ' 2' … ' 9') if required. Note the apostrophes do not print.
  • %j: Prints the twenty-four hours of the yr, with upwards to two leading zeroes, if required.

Output of the date command with a A u w d e j options

Options to Display the Week

  • %U: Prints the week number of twelvemonth, considering Sunday equally the first 24-hour interval of the week. For case, the third week of the yr, twentieth week of the year, etc.
  • %Five: Prints the ISO week number of the yr, because Monday every bit the get-go day of the week.
  • %W: Week number of the twelvemonth, because Monday as the first twenty-four hour period of the week.

Output of the date command with U V W options

Options to Display the Calendar month

  • %b or %h: Prints the proper name of the month abbreviated to January, Feb, Mar, etc.
  • %B: prints the full name of the month, January, Feb, March, etc.
  • %m: Prints the number of the calendar month, with a leading zero if required 01, 02, 03 … 12.

Output of the date command with b h B m options

Options to Display the Twelvemonth

  • %C: Prints the century without the year. In 2022 it would print 20.
  • %y: Prints the yr every bit two digits. in 2022 it volition print 19.
  • %Y: Prints the twelvemonth as four digits.

Output of the date command with C y Y options

Options to Brandish the Time

  • %T: Prints the time as HH:MM:SS.
  • %R: Prints the 60 minutes and minutes as HH:MM with no seconds, using the 24-60 minutes clock.
  • %r: Prints the time according to your locale, using the 12-60 minutes clock and an am or pm indicator.
  • %10: Prints the fourth dimension co-ordinate to your locale, using the 24-hr clock. Allegedly. Note that during testing this pick behaved exactly equally %r does, as shown below. On a Linux automobile configured for the U.k. locale and set to GMT, it printed the time, using the 24-hr clock with no AM or PM indicator, as expected.

Output of the date command with T R r X options

Options to Display the Hour

  • %H: Prints the 60 minutes 00, 01, 02…23.
  • %I: Prints the 60 minutes using the 12-hour clock, 00, 01, 02 … 12, with a leading zero if required.

Output of the date command with H I options

Options to Display Minutes

  • %Chiliad: prints the infinitesimal, 01, 02, 03  … 59, with a leading zero if required.

Output of the date command with M options

Options to Display Seconds

  • %s: Prints the number of seconds since 1970-01-01 00:00:00, the showtime of the Unix Epoch.
  • %Due south: Prints the seconds, 01, 02, 03 … 59, with a leading cipher if required.
  • %N: Prints the Nanoseconds.

Output of the date command with s S N options

Options to Display Timezone Information

  • %z: Prints the time deviation between your timezone and UTC.
  • %:z: Prints the time deviation between your timezone and UTC, with a : betwixt the hours and minutes. Note the : between the % sign and z .
  • %::z: Prints the time deviation between your timezone and UTC, with a : between the hours, minutes and seconds. Annotation the :: betwixt the % sign and z .
  • %Z: Prints the alphabetic timezone proper name.

Output of the date command with timezone options

Options Related to Formatting

  • %p: Prints the AM or PM indicator in uppercase.
  • %P: Prints the am or pm indicator in lowercase. Note the quirk with these 2 options. A lowercase p gives capital letter output, an majuscule P gives lowercase output.
  • %t: Prints a tab.
  • %n: Prints a new line.

Output of the date command with AM PM indicator and formatting options

Options to Alter Other Options

These modifiers can be inserted between the % and the option letter of other options to alter their brandish. For instance, %-S would remove the leading zero for single-digit seconds values.

  • : A unmarried hyphen prevents zero padding on single digit values.
  • _: a single underscore adds leading spaces for single digit values.
  • 0: Provides leading zeroes for single digit values.
  • ^: Uses capital, if possible (not all options respect this modifier).
  • #: Utilise the contrary to the default case for the choice, if possible (not all options respect this modifier).

Output of the date command with formatting options

Two More Corking Tricks

To go the terminal modification time of a file, utilise the -r (reference) option. Note that this uses a - (hyphen) instead of a % sign, and it doesn't require a + sign. Attempt this command in your home folder:

appointment -r .bashrc

Output of the date command with file modification time option

The TZ setting allows you to modify your timezone for the duration of a unmarried command.

TZ=GMT date +%c

Output of the date command for a different timezone

Using Date in Scripts

Enabling a Bash shell script to print the fourth dimension and date is trivial. Create a text file with the following content, and salvage information technology as gd.sh.

#!/bin/fustigate  TODAY=$(appointment +"Today is %A, %d of %B") TIMENOW=$(date +"The local time is %r") TIME_UK=$(TZ=BST engagement +"The time in the United kingdom of great britain and northern ireland is %r")  echo $TODAY repeat $TIMENOW repeat $TIME_UK

Blazon the following command to set the execution permissions and make the script executable.

chmod +x gd.sh

Run the script with this control:

./gd.sh

Output of the gd.sh script

We tin use the date command to provide a timestamp. The script shown volition create a directory with the timestamp as its name. Information technology will and then re-create all text files from the electric current binder into information technology. By running this script periodically we can take a snapshot of our text files. Over fourth dimension we'll build up a series of folders with different versions of our text files in them.

Note that this isn't a robust backup organisation, it's merely for illustrative purposes.

Create a text file with the post-obit content, and relieve it as snapshot.sh.

#!/bin/bash  # obtain the date and time date_stamp=$(date +"%F-%H-%G-%S")  # brand a directory with that name mkdir "$date_stamp"  # re-create the files from the electric current folder into it cp *.txt "$date_stamp"  # all washed, written report back and go out echo "Text files copied to directory: "$date_stamp

Type the following command to set the execution permissions and make the script executable.

chmod +x snapshot.sh

Run the script with this command:

./snapshot.sh

Effect of running the snapshot.sh script

You'll see that a directory has been created. Its name is the appointment and time at which the script was executed. Inside that directory are copies of the text files.

Given a bit of thought and inventiveness, even the apprehensive appointment command can be put to productive use.

Source: https://www.howtogeek.com/410442/how-to-display-the-date-and-time-in-the-linux-terminal-and-use-it-in-bash-scripts/

0 Response to "How To Set Date In Terminal Mac"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel