Linux date formatting example

Linux & Unix Date Format Examples

Learn about Linux and Unix date & time formating with the help of simple explanation and added example. Date command in Unix & Linux is used to format date as well as time. First portion of this article covers Date function and the second portion has the time function covered.

Linux & Unix Basic date format

Where Parameters is any one or more Parameter from the list below and separator is a any field separator like hyphen( – ) , slash , colon : and it is optional.

Linux & Unix Date Format Examples

lets get the parameter for year , month and date and produce a hyphen separated date format

$date “+%Y-%m-%d”
2010-02-10
%Y is keyword for four digit year
%m is keyword for two digit month
%d is keyword for two digit date
Hyphen is separator here .

If you change the separator here to slash the format would look like

You can switch date keyword anyway you want , if you need month first and year last ,

Weekdays

You can have additional weekday information in your date with %A as

These are options you can select for weekday

%a weekday, abbreviated Wed
%A weekday, full Wednesday
%d day of the month (dd), zero padded 10
%e day of the month (dd) 10
%j day of year, zero padded 000-366
%u day of week starting with Monday (1), i.e. mtwtfss 3
%w day of week starting with Sunday (0), i.e. smtwtfs 3

Month

You can have additional month information in your date with %B as

These are options you can select for month

%m mm month as two digits
%b Mon, locale’s abbreviated Feb
%B locale’s full month, variable length February

Year

You can have additional year information as two digit or four digit with %y or %Y as

These are options you can select for year

%y yy two digit year 00–99
%Y ccyy year 2010

Additional date format parameters which can be used :

Week

%U week number Sunday as first day of week 01–53
%W week number Monday as first day of week 01–53
%V week of the year 01–53

Century

cc or %C century prints only last two digits (e.g., 20)

Date

%D mm/dd/yy 02/10/10
%x locale’s date representation (mm/dd/yy) 02/10/2010
%F %Y-%m-%d 2010-02-10

Date and Time time stamp

%c locale’s date and time Wed Feb 10 10:02:33 PST 2010

Time function

Time function is simple to use and date command is used to format & display time as well . You can use date and time parameters to format your date time stamp as per your requirment

Читайте также:  Основные консольные команды linux

A simple time stamp with date can be generated as :

$date “+%c”
%c locale’s date and time Sat Nov 04 12:02:33 EST 1989

Date function have some preformated time stamps to make things easier

%R hours, minutes (24 hour clock) hh:mm e.g. 17:28
%T hours, minutes, seconds (24-hour clock) 17:28:55
%X locale’s time representation (%H:%M:%S) 05:28:55 PM

Time stamp with 12 hr clock

$date “+%l:%M:%S
5:22:45

Time stamp with 24 hr clock

$date “+%H:%M:%S”
20:22:45

Time stamp with AM/PM

$date “+%l:%M:%S %p”
5:22:45 PM

More date time parameters :

Hours

%l (Lowercase L) hour (12 hour clock) 8
%I (Uppercase I) hour (12 hour clock) zero padded 08
%k hour (24 hour clock) 20
%H hour (24 hour clock) zero padded 20
%p locale’s upper case AM or PM (blank in many locales) PM
%P locale’s lower case am or pm (really!) pm

Minutes

Seconds

%s seconds since 00:00:00 1970-01-01 UTC (Unix epoch) 1265833735
%S SS second 00–60
%N nanoseconds 000000000–999999999

Time zone

%z -zzzz RFC-822 style numeric timezone -0500
%Z time zone (e.g., EDT) nothing if no time zone is determinable EST

Comments Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Источник

Date Command in Linux: How to Set, Change, Format and Display Date

Linux date command displays and sets the system date and time. This command also allows users to print the time in different formats and calculate future and past dates.

Read on to learn how to use the date command in Linux.

How to use Linux date command

  • A system running Linux
  • A user account with root privileges
  • Access to a terminal window/command line

Linux date Command Syntax

The syntax for the date command is:

How to Use date Command in Linux

To show the current system time and date, type in the date command:

Date command in linux

The output displays the day of the week, day of the month, month, year, current time, and time zone. By default, the date command is set to the time zone of the operating system.

The -d option allows users to operate on a specific date. For example, we can type in the following command:

See an older date

You can use the —date command to display the given date string in the format of a date. This command does not affect the system’s actual date and time values, and it only prints the requested date. For example:

Pull a date from a string

Note: Learn how can you create a script using the printf command to display the current date.

Linux date Command Format Options

To format the date command’s output, you can use control characters preceded by a + sign. Format controls begin with the % symbol and are substituted by their current values.

Here, the %Y character is replaced with the current year, %m with month, and %d with the day of the month:

date +"Year: %Y, Month: %m, Day: %d"

Format the current date

Here are another two formatting examples:

Another date format example

date +"Week number: %V Year: %y"

Another formatting example

These are the most common formatting characters for the date command:

    • %D – Display date as mm/dd/yy
    • %Y – Year (e.g., 2020)
    • %m – Month (01-12)
    • %B – Long month name (e.g., November)
    • %b – Short month name (e.g., Nov)
    • %d – Day of month (e.g., 01)
    • %j – Day of year (001-366)
    • %u – Day of week (1-7)
    • %A – Full weekday name (e.g., Friday)
    • %a – Short weekday name (e.g., Fri)
    • %H – Hour (00-23)
    • %I – Hour (01-12)
    • %M – Minute (00-59)
    • %S – Second (00-60)

    To see all formatting options, run date —help or the man command man date in your terminal.

    Set or Change Date in Linux

    To change the system clock manually, use the set command. For example, to set the date and time to 5:30 PM, May 13, 2010, type:

    Manual time set in Linux

    Most Linux distributions have the system clock synchronized using the ntp or the systemd-timesyncd services, so be careful when the setting the clock manually.

    Display Past Dates

    Use the —date option to display past dates in Linux. The date command accepts values such as «tomorrow» , «Friday» , «last Friday» , «next Friday» , «next week» , and similar. So, use the following strings to print past dates::

    Format to

    Yesterday

    Time ten seconds ago format in linux

    Display Future Dates

    The —date option can also display future dates. Like with past dates, you can type in strings to print upcoming dates:

    Date next monday format in linux

    Date four days from now format in linux

    Tomorrow

    Display the Date String at Line of File

    The —file option prints the date string present at each line of the file. Unlike the —date option, —file can present multiple date strings at each line.

    This is the syntax for the —file command:

    Here we use the cat command to add dates to a file and then print them with the date command:

    Display dates at each file line in linux

    Display Last Modified Timestamp of a Date File

    When you use the -r option, the date command prints the last modification time of a file. For example, the following command prints the last time the hosts file was changed:

    The last date the file was modified in linux

    Override a Time Zone

    By default, the date command uses the time zone defined in /etc/localtime . To use a different time zone in the environment, set the TZ variable to the desired time zone.

    For example, to switch to New York time, enter:

    Switch to New York time zone in Linux

    Type in the date command to return the system to its default time zone. To see all available time zones, use the timedatectl list-timezones command.

    The date command can also show the local time for a different time zone. For example, to display the local time for 4:30 PM next Monday on the Australian east coast, type:

    date -d 'TZ="Australia/Sydney" 04:30 next Monday'

    Switch to South Australian time in Linux

    Use date with Other Commands

    You can use the date command to create file names that contain the current time and date. The input below creates a backup MySQL file in the format of the current date:

    mysqldump database_name > database_name-$(date +%Y%m%d).sql

    Another common use of the date command is in shell scripts. Below we assign the output of date to the date_now variable:

    Echo the date now variable

    Use Unix Epoch Time (Epoch Converter)

    You can use the date command as an Epoch converter. Epoch, or Unix timestamps, is the number of seconds that have passed since January 1, 1970, at 00:00:00 UTC.

    To show the number of seconds from the epoch to the current day, use the %s format control:

    Epoch seconds in linux

    To see how many seconds passed from epoch to a specific date, enter:

    Epoch seconds from a specific date in Linux

    You now have a good understanding of how to use the date command in Linux. If you are interested in more date/time configuration options for Linux, read How to Set or Change Timezone/Date/Time on Ubuntu.

    Источник

    12 Useful Linux date Command Examples

    Linux Date Command Examples

    The date command is a command-line utility for displaying or setting date and time in the Linux system. It uses the system default time zone to display the time.

    In this article, I will show you 12 examples of how to best use the date command on Linux. To demonstrate the examples below I have used an Ubuntu 20.04 system. As the date command is pre-integrated in all Linux systems we don’t need to install it.

    Date Command Examples

    Displaying Date

    By default, the date command will display the current system date and time in a default format.

    Display current date

    Current date of the system.

    Displaying Universal Time

    If your system time zone is based on your local time zone and you want to check the universal time, to do so we need to add the -u option to the command which refers to UTC.

    Display UTC time and date

    Custom Date Format

    We can overwrite the default date format with our preferred date format. To achieve that we need to add a format control character led by + sign and format control begins with the % sign. Some of the most used date format control characters are:

    • %a – Locale’s abbreviated short weekday name (e.g., Wed)
    • %A – Locale’s abbreviated full weekday name (e.g., Wednesday)
    • %b – Locale’s abbreviated short month name (e.g., Jun)
    • %B – Locale’s abbreviated long month name (e.g., June)
    • %Y – Display Year (e.g., 2021)
    • %m – Display Month (01-12)
    • %d – Day of month (e.g., 02)
    • %D – Display date as mm/dd/yy
    • %H – Hour in 24 hr format (00-23)
    • %I – Hour in 12 hr format (01-12)
    • %M – Display Minute (00-59)
    • %S – Display Second (00-60)
    • %u – Day of the week (1-7)

    Here, in the following example, we formatted the date in yyyy-MM-dd format.

    Use a custom date format

    Custm date format without time

    Displaying Date From String

    We can display the formatted date from the date string provided by the user using the -d or –date option to the command. It will not affect the system date, it only parses the requested date from the string. For example,

    Pass a string containing a date to date command

    Parsing string to date

    Displaying Upcoming Date & Time With -d Option

    Aside from parsing the date, we can also display the upcoming date using the -d option with the command. The date command is compatible with words that refer to time or date values such as next Sun, last Friday, tomorrow, yesterday, etc. For examples,

    Displaying Next Monday Date

    Display futire dates and date calculations

    Displaying Past Date & Time With -d Option

    Using the -d option to the command we can also know or view past date. For examples,

    Displaying Last Friday Date

    Parse Date From File

    If you have a record of the static date strings in the file we can parse them in the preferred date format using the -f option with the date command. In this way, you can format multiple dates using the command. In the following example, I have created the file that contains the list of date strings and parsed it with the command.

    Parse multiples dates from file

    Setting Date & Time on Linux

    We can not only view the date but also set the system date according to your preference. For this, you need a user with Sudo access and you can execute the command in the following way.

    $ sudo date -s "Sun 30 May 2021 07:35:06 PM PDT"

    Display File Last Modification Time

    We can check the file’s last modification time using the date command, for this we need to add the -r option to the command. It helps in tracking files when it was last modified. For example,

    Display file modification time

    Override the System Timezone

    The date command will display the date according to your configured system time zone. We need to set the TZ variable to the desired time zone to use various time zones in the environment. For example, to switch to New York time, execute:

    Use TZ variable to override time zone

    Date with prefer time zone

    To see all available time zones, use the timedatectl list-timezones command.

    Use Unix Epoch Time

    Epoch time is the number of seconds that have passed since January 1, 1970, at 00:00:00 UTC. We can use %s format control to view the number of seconds from epoch time to current time.

    Seconds since since January 1, 1970, at 00:00:00 UTC

    Using Date in File Naming

    We can create files with the current date which helps in keeping the track record of the file. In the following example, I have created a file including a current date in its name.

    Use date in file names

    Naming file with the date.

    Conclusion

    In this article, we learn how to use the date command and how to pare send format dates on Linux.

    About This Site

    Vitux.com aims to become a Linux compendium with lots of unique and up to date tutorials.

    Latest Tutorials

    Источник

Оцените статью
Adblock
detector