≡ Menu

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

The following example takes an input date and time string, and displays the output in date format.

$ 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

It displays date in which 10 seconds are elapsed since epoch 1970-01-01 UTC:

$ date --date=@10
Wed Dec 31 16:00:10 PST 1969

It displays date in which 1 minute (i.e. 60 seconds) is elapsed since epoch 1970-01-01 UTC:

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

4. Display Past Date

You can display a past date using the -date command. Few possibilities are shown below.

$ 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

You can display date in UTC format using -u, or –utc, or –universal option as shown below.

$ 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

In this example, the current time is 20:25:48

$ 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.

$ touch datefile

The current time after the above touch command is 20:26:12

$ 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

You can use formatting option to display date command in various formats using the following syntax:

$ date +%<format-option>

The following table displays various date command formatting options.

Format options Purpose of Option Output
date +%a Displays Weekday name in short (like Mon, Tue, Wed) Thu
date +%A Displays Weekday name in full short (like Monday, Tuesday) Thursday
date +%b Displays Month name in short (like Jan, Feb, Mar ) Feb
date +%B Displays Month name in full short (like January, February) February
date +%d Displays Day of month (e.g., 01) 07
date +%D Displays Current Date; shown in MM/DD/YY 02/07/13
date +%F Displays Date; shown in YYYY-MM-DD 2013-02-07
date +%H Displays hour in (00..23) format 23
date +%I Displays hour (01..12) format 11
date +%j Displays day of year (001..366) 038
date +%m Displays month (01..12) 02
date +%M Displays minute (00..59) 44
date +%S Displays second (00..60) 17
date +%N Displays nanoseconds (000000000..999999999) 573587606
date +%T Displays time; shown as HH:MM:SS
Note: Hours in 24 Format
23:44:17
date +%u Displays day of week (1..7); 1 is Monday 4
date +%U Displays week number of year, with Sunday as first day of week (00..53) 05
date +%Y Displays full year i.e. YYYY 2013
date +%Z alphabetic time zone abbreviation (e.g., EDT) IS
Add your comment

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

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Jalal Hajigholamali May 21, 2013, 10:27 am

    Hi,

    Thanks a lot,

    simple and very useful article

  • Mukesh May 21, 2013, 10:38 am

    Its really useful

  • Bob May 21, 2013, 12:48 pm

    Great article yet again.

  • Paul May 22, 2013, 3:30 am

    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

  • Don Ellis May 28, 2013, 2:47 pm

    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!

  • Carlz May 30, 2013, 11:14 pm

    Nice One .. this is what is really need for may scripting.. thank you thank you !!!!!

  • shweta June 8, 2013, 3:57 am

    very nice options……… thanx for sharing… 🙂

  • Sakshi June 11, 2013, 12:17 pm

    Great article yet again.

  • prafull October 10, 2013, 11:57 pm

    Good one….informative and super job!!

  • Rich January 5, 2015, 7:02 am

    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.

  • anuj February 22, 2015, 10:55 pm

    thanks

  • Evgeny March 30, 2015, 7:14 am

    thank you

  • Alex April 15, 2015, 6:26 am

    HI

    How to find difference between two dates:
    start=$(date +%T)
    smth is done
    end=$(date +%T)
    diff=$(( $end-$start ))

    Thank you in advance!

  • Logan May 11, 2015, 5:31 pm

    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

  • Logan May 11, 2015, 7:04 pm

    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 {0..3}; do date +%Y%m%d –date “$i day”; done
    20150511
    20150512
    20150513
    20150514

  • Logan May 11, 2015, 7:08 pm

    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

  • Surya May 11, 2016, 8:14 am

    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??

  • Anonymous March 8, 2017, 5:35 am

    Its very useful.