Display date on linux

7 Linux Date Command Examples to Display and Set System Date Time

Date command is helpful to display date in several formats. It also allows you to set systems date and time. This article explains few examples on how to use date command with practical examples.

When you execute date command without any option, it will display the current date and time as shown below.

$ date Mon May 20 22:02:24 PDT 2013

1. Display Date from a String Value using –date Option

If you have a static date or time value in a string, you can use -d or –date option to convert the input string into date format as shown below. Please note that this doesn’t use the current date and time value. Instead is uses the date and time value that you pass as string. The following examples takes an input date only string, and displays the output in date format. If you don’t specify time, it uses 00:00:00 for time.

$ date --date="12/2/2014" Tue Dec 2 00:00:00 PST 2014 $ date --date="2 Feb 2014" Sun Feb 2 00:00:00 PST 2014 $ date --date="Feb 2 2014" Sun Feb 2 00:00:00 PST 2014
$ date --date="Feb 2 2014 13:12:10" Sun Feb 2 13:12:10 PST 2014

2. Read Date Patterns from a file using –file option

This is similar to the -d or –date option that we discussed above. But, you can do it for multiple date strings. If you have a file that contains various static date strings, you can use -f or –file option as shown below.

In this example, we can see that datefile contained 2 date strings. Each line of datefile is parsed by date command and date is outputted for each line.

$ cat datefile Sept 9 1986 Aug 23 1987 $ date --file=datefile Tue Sep 9 00:00:00 PDT 1986 Sun Aug 23 00:00:00 PDT 1987

3. Get Relative Date Using –date option

You can also use date command to get a future date using relative values. For example, the following examples gets date of next Monday.

$ date --date="next mon" Mon May 27 00:00:00 PDT 2013

If string=@is given to date command, then date command convert seconds since the epoch (1970-01-01 UTC) to a date. It displays date in which 5 seconds are elapsed since epoch 1970-01-01 UTC:

$ date --date=@5 Wed Dec 31 16:00:05 PST 1969
$ date --date=@10 Wed Dec 31 16:00:10 PST 1969
$ date --date=@60 Wed Dec 31 16:01:00 PST 1969

4. Display Past Date

$ date --date='3 seconds ago' Mon May 20 21:59:20 PDT 2013 $ date --date="1 day ago" Sun May 19 21:59:36 PDT 2013 $ date --date="yesterday" Sun May 19 22:00:26 PDT 2013 $ date --date="1 month ago" Sat Apr 20 21:59:58 PDT 2013 $ date --date="1 year ago" Sun May 20 22:00:09 PDT 2012

5. Set Date and Time using –set option

You can set date and time of your system using -s or –set option as shown below.. In this example, initially it displayed the time as 20:09:31. We then used date command to change it to 21:00:00.

$ date Sun May 20 20:09:31 PDT 2013 $ date -s "Sun May 20 21:00:00 PDT 2013" Sun May 20 21:00:00 PDT 2013 $ date Sun May 20 21:00:05 PDT 2013

5. Display Universal Time using -u option

$ date Mon May 20 22:07:53 PDT 2013 $ date -u Tue May 21 05:07:55 UTC 2013

6. Display Last Modification Time using -r option

$ date Sun May 20 20:25:48 PDT 2013

The timestamp of datefile is changed using touch command. This was done few seconds after the above date command’s output.

$ date Sun May 20 20:26:12 PDT 2013

Finally, use the date command -r option to display the last modified timestamp of a file as shown below. In this example, it displays last modified time of datefile as 20:25:57. It is somewhere between 20:25:48 and 20:26:12 (which is when we execute the above touch command to modify the timestamp).

$ date -r datefile Sun May 20 20:25:57 PDT 2013

7. Various Date Command Formats

If you enjoyed this article, you might also like..

Comments on this entry are closed.

Читайте также:  Astra linux удаленный доступ anydesk

Hi,
Wanted to know that is that possible to substact time ( may be day/second/minutes) in reference to a old date, ie, suppose old date=7th Jan 2013 and i want to get the date of 3 days prior to 7th Jan 2013

I’ve been participating in a local discussion list on this topic. Most of the examples use GNU date, but MacOS X (and BSD) uses BSD date. I looked at HomeBrew for a GNU date for the Mac, but have only found complaints that it isn’t available. Any better answers for getting GNU date on BSD variants? Correction: I found a HomeBrew formula for date included in the coreutils package. Prepending /usr/local/opt/coreutils/libexec/gnubin to PATH allows using the common names for the utilities instead of the default of requiring prepending ‘g’ to each command’s name.
I had found this a week ago, but was interrupted in implementing it. Installing coreutils and prepending that path (and similar path for man) has given me a whole set of GNU utilities to explore!

great list. here is one more that isn’t documented much. Timezone conversion. Convert time from another time zone to locale:
date -d ‘jan 06 8:00 KST’
Mon Jan 5 18:00:00 EST 2015 Convert locale time to another time zone:
TZ=KST date
Mon Jan 5 12:57:43 KST 2015 Here is a good site with all the time zone abbreviations.

HI How to find difference between two dates:
start=$(date +%T)
smth is done
end=$(date +%T)
diff=$(( $end-$start )) Thank you in advance!

Here are some tricks to generate date sequences in varying intervals (day/week/month/year) either upwards or downwards. In the examples below, 20141229 could be specified also as 2014-12-29 or 2014/12/29 $for i in <0..3>; do date +%Y%m%d –date “20141229 $i day”; done
20141229
20141230
20141231
20150101 $for i in <0..3>; do date +%Y%m%d –date “20141229 $i week”; done
20141229
20150105
20150112
20150119 $for i in <0..3>; do date +%Y%m%d –date “20141229 $i month”; done
20141229
20150129
20150301
20150329 $for i in <0..3>; do date +%Y%m%d –date “20141229 $i year”; done
20141229
20151229
20161229
20171229 $for i in <0..3>; do date +%Y%m%d –date “20141229 $i day ago”; done
20141229
20141228
20141227
20141226 $for i in <0..3>; do date +%Y%m%d –date “20141229 $i week ago”; done
20141229
20141222
20141215
20141208 $for i in <0..3>; do date +%Y%m%d –date “20141229 $i month ago”; done
20141229
20141129
20141029
20140929 $for i in <0..3>; do date +%Y%m%d –date “20141229 $i year ago”; done
20141229
20131229
20121229
20111229 # Last day of the month (upwards)
$for i in <0..3>; do date +%Y%m%d –date “20141229 $i month 29 day ago”; done
20141130
20141231
20150131
20150228 # Last day of the month (downwards)
$for i in <0..3>; do date +%Y%m%d –date “20141229 $i month ago 29 day ago”; done
20141130
20141031
20140930
20140831 $for i in <0..3>; do date +%Y%m%d –date “20141229 $i month ago 1 week ago”; done
20141222
20141122
20141022
20140922

Читайте также:  Hardware configuration in linux

Forgot to mention. In the above, if you remove the hardcoded date, 20140249, the current date will be used. $ date
Mon May 11 17:07:46 PDT 2015 $ for i in ; do date +%Y%m%d –date “$i day”; done
20150511
20150512
20150513
20150514

Last day of the month $ for i in <0..3>; do date +%Y%m%d –date “$i month `date +%d` day ago”; done
20150430
20150531
20150630
20150731 $ for i in <0..3>; do date +%Y%m%d –date “$i month ago `date +%d` day ago”; done
20150430
20150331
20150228
20150131

This looks Very useful 🙂 If i give Date as input , I would like to get it’s week start date and week end date… For eg:- If i give 20160301 it’s week start date is 20160229 and it’s week end date is 20160306 …. How can i get this??

Источник

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.

    Источник

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