UNIX Date Command Examples

How do I view and set date under UNIX operating systems? How do I see the current time/date on Unix based server?

The date command under UNIX displays date and time. You can use the same command set date and time. You must be the super-user (root) to change the date and time on Unix like operating systems.
Tutorial details
Difficulty Easy (rss)
Root privileges Yes
Requirements None
Time 5m
The date command shows the date and time read from the kernel clock.

UNIX Date Command Examples and Syntax

The syntax is:

date 
date "+format"

Display Current Date and Time

Type the following command:

date

Sample outputs:

Tue Oct 27 15:35:08 CDT 2009

When executed without arguments, the date command shows the current date and time.

Set The Current Time

You must run command as root user. To set the current time to 05:30:30, enter:

date 0530.30

Warning: Please don’t change the Unix date and time when the server is running with more than one user (multi-user mode) or critical services, as it may result in unexpected behaviors.

Set Date

The syntax is as follows:
date mmddHHMM[YYyy] date mmddHHMM[yy]

Set the date to Oct 25, 12:45 a.m., enter:

date 10250045

Again you must run command as root user. In this example, set the current date and time to Oct 15, 2009 04:30 you type:

date --set="20091015 04:30"

Generating Output

WARNING! These examples may not work on Linux computer running GNU/coreutiles date command. All examples are tested on HP-UX, AIX, Sun Solaris and other proprietary UNIX operating systems only.

Type the following command:

date '+DATE: %m/%d/%y%nTIME:%H:%M:%S'

Sample outputs:

DATE: 10/27/09
TIME:15:50:44

Try the following examples:

date "+%m/%d/%y" 
date "+%Y%m%d"
date +'%-4.4h %2.1d %H:%M'

Understanding date format strings

From the date command man page:

  • %a – Displays the locale’s abbreviated weekday name.
  • %A – Displays the locale’s full weekday name.
  • %b – Displays the locale’s abbreviated month name.
  • %B – Displays the locale’s full month name.
  • %c – Displays the locale’s appropriate date and time representation (default).
  • %C – Displays the first 2 digits of the four-digit year as a decimal number (00-99). A year is divided by 100 and truncated to an integer.
  • %d – Displays the day of the month as a decimal number (01-31). In a two-digit field, a 0 is used as leading space fill.
  • %D – Displays the date in the format equivalent to %m/%d/%y.
  • %e – Displays the day of the month as a decimal number (1-31). In a two-digit field, a blank space is used as leading space fill.
  • %h – Displays the locale’s abbreviated month name (a synonym for %b).
  • %H – Displays the hour (24-hour clock) as a decimal number (00-23).
  • %I – Displays the hour (12-hour clock) as a decimal number (01-12).
  • %j – Displays the day of year as a decimal number (001-366).
  • %k – Displays the 24-hour-clock hour clock as a right-align, space-filled number (0 – 23).
  • %m – Displays the month of year as a decimal number (01-12).
  • %M – Displays the minutes as a decimal number (00-59).
  • %p – Displays the locale’s equivalent of either AM or PM.
  • %r – Displays 12-hour clock time (01-12) using the AM-PM notation; in the POSIX locale, it is equivalent to %I:%M:%S %p.
  • %S – Displays the seconds as a decimal number (00 – 59).
  • %T – Displays the 24-hour clock (00-23) in the format equivalent to HH:MM:SS.
  • %w – Displays the weekday as a decimal number in the range 0-6 (Sunday = 0). Refer to the %u field descriptor.

Unix Command Help

Type the following man command to read the date command man page:
man date

Conclusion

We learned about valorous Unix date command examples and syntax to display or set date and time.

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
26 comments… add one
  • Philippe Petrinko Mar 15, 2010 @ 16:32

    @Vivek – Do you confirm this one ? My date ((GNU coreutils) 6.10) does not like it much.

    date +'%-4.4h %2.1d %H:%M'

    What does it do on your system?

  • 🐧 nixCraft Mar 15, 2010 @ 18:15

    @Philippe,

    I think I used that one on proprietary AIX or may be on HP-UX UNIX box. The month field is four characters long, left side. Same goes for the day (2 chars long). It will not work on *GNU coreutils*. You can try them on HP-UX or AIX and should provide output as follows:

    mmmm    dd HH:MM
  • Philippe Petrinko Mar 15, 2010 @ 18:19

    @Vivek: Then, would you consider adding some warning/advice/comment to prevent your readers loosing time (and hairs ;-) ).

  • 🐧 nixCraft Mar 15, 2010 @ 18:44

    Title does says “UNIX Date Command Examples” and Linux != UNIX. Nevertheless, your suggestion is accepted :). Thanks!

  • priyanka Jul 5, 2011 @ 10:25

    very nice contain in this site……….useful very much in study…………..

  • learner Jul 13, 2011 @ 23:04

    will date --version work in linux? why is the output different that
    echo "--version" | date

    • Philippe Petrinko Jul 14, 2011 @ 14:20

      Hi Learner,

      Right way to give parameters on command line is:

      date --version

      When you want a program to use some input from a pipe is:

      echo "some text used as input" | tr "[[:lower:]]" "[[:upper:]]"

      But these are two really different functionalities. They are not equivalent at all, so they cannot yield the same.

      — Philippe

      • Learner Jul 14, 2011 @ 14:24

        Consider – echo “–version”| date
        here “echo –version” will give the output –version , which will act as an input to date. so we should get date –version.

        But we do not get the same output from echo “–version” | date
        and date –version. Can you please explain why the output is different.

        • Philippe Petrinko Jul 14, 2011 @ 15:53

          You confuse [standard input usage] with [parameters on the command line] which are different kind of inputs, which cannot be exchanged.

          The genuine way to give parameters to a command is to give them on the program call, which is made on the command line.

          There a other ways to achieve parameter passing to a command, such as using a specific file, or using environment variables.

          On the other hand, standard input is commonly used to give _data_ to process, not parameters.

          As a matter of facts, [date] command does make any use of standard input.
          So you can pipe _anything_ you want, [date] won’t ever use it. You might have pipe (with echo) any text, [date] won’t use it.

          Make sure by Reading The Fantastic Manual,
          by issuing the command: man date

          Do you think you get the point?

  • amsurre Feb 22, 2012 @ 13:14

    How to overcome missing -d option (present on gnu date – linux) on unix based OS ..

    date -d '1 hour ago''

    • Philippe Petrinko Feb 22, 2012 @ 21:49

      Hi vivek,

      Typo here: “other propitiatory UNIX operating systems only”
      “propitiatory” should be: “proprietary”

      ;-)
      KUTGW

      • 🐧 nixCraft Feb 23, 2012 @ 6:25

        Thanks for the heads up!!!

      • bharath Jan 16, 2013 @ 10:50

        I am using these 2 commands in my unix shell sxript. when i do subtract , and if current month is jan, the jan -1 month should give me dec 2012, but getting it wrong. can you correct me?

        mydate=`date +%Y-%m-%d`
        mydate1=`date +%Y`’-0′$((`date +%m`-1))’-‘`date +%d`

        OrderedDate BETWEEN
        DATE(‘$mydate1 HKT’) AND
        DATE(‘$mydate HKT’)

    • 🐧 nixCraft Feb 23, 2012 @ 6:25

      GNU has more advantages over the traditional Unix date command. Try:

      yest

      Try TZ to get y’day and tomorrow:

      TZ=IST24 date   # y'day
      TZ=IST-24 date # tomorrow
      

      Where,

      • IST is my timezone and 24 is offset.

      Write a perl / shell / python script to calculate date :)

    • CBee Jan 16, 2013 @ 10:51

      Since about 15 years, if Ineed to maintain multiple unix variants (sun/solaris, hp-ux etc), I most frequently find myself installing the gnu tools and using them. They are available for all unix system that come with a c-compiler…

  • C. Beerse Apr 4, 2012 @ 21:32

    Nice page on unix date command. Actually I was looking for the formats to be used by the –set=”” and/or –date=”” options as available on cygwin and/or linux.

    Reason: I intend to use `date -date="input date spec" "+%Y%m%d%H%M%S"` to translate date specifications to the format I like/need.

    I already found `date --date=@12345678` which expects the epoc time (seconds since 1 jan 1970). Also nice are ‘yesterday’, ‘now’, ‘tomorrow’, ‘last friday’ and such.

  • myName Aug 30, 2012 @ 11:15

    Thanks

  • bharath Jan 16, 2013 @ 9:49

    I am using these 2 commands in my unix shell sxript. when i do subtract , and if current month is jan, the jan -1 month should give me dec 2012, but getting it wrong. can you correct me?

    mydate=`date +%Y-%m-%d`
    mydate1=`date +%Y`'-0'$((`date +%m`-1))'-'`date +%d`

    OrderedDate BETWEEN
          DATE('$mydate1 HKT') AND
           DATE('$mydate HKT')
    • CBee Jan 16, 2013 @ 11:05

      You are twiggling with the output using text in the output string. Keep in mind, the main purpose of the command `date` is to fetch the epoc (seconds since 00:00gmt, 1 jan 1970) and to translate that to the human readable version in the current timezone.

      With that, most date commands can use different input to be used in stead of the current epoc. gun date (gnu tools, linux, cygwin etc.) does have a -d option to use an alternate date. There you can use `date -d lastmonth` to get the date from a month ago. On others you might only be able to use an alternate epoc. Then you can use (up to a limit) the current epoc minus the number of seconds in a month. To make your script easy, best use 4 or 5 weeks for this (5 * 24 * 60 * 60)

  • Shree Duth Awasthi Jun 28, 2013 @ 17:34

    I have two requirements, I want to modify the system time by (a)1 sec (b) More than 1000 sec and observe some behaviour.

    Can you please suggest in unix shell scripts ?

  • RunnySpoon Aug 30, 2013 @ 15:40

    Hey, I realise that this is quite an old topic, but hopefully you’re still watching it …

    I am trying to write a cp command and pipe in the current date/time into the target filename. Something along the lines of:
    cp sourcefile.txt sourcefile_20130830113025.txt

    Is this possible in a single cp command?

    I realise that I can format the date using date “+%Y%m%d%H%M%S”, I just can’t figure out how to get that in the middle of my filename. I’m sure it’s probably really simple and I just haven’t had enough coffee yet.
    (Solaris 5.10)

    • Philippe Petrinko Aug 31, 2013 @ 8:10

      @RunnySpoon.
      Hey to you also.

      I hope you finally put a hand on a coffee mug. I’ll welcome you visiting me for a coffee, too. In the meantime, I give you this:

      All you need is “Command substitution”:
      Take a look at [ https://bash.cyberciti.biz/guide/Command_substitution ]

      Try to do it yourself, and come back to us with your work to share it back, we’ll appreciate that – and we’ll give you a hint if you need.

      Cheers

      –P

    • cbeerse Aug 31, 2013 @ 16:19

      To use the output of 1 command on the commandline of an other one, you need to use teh “ quotes. (back quotes, on an US keyboard most likely found left to the key with 1 and !).

      If the current date is given with the next command:

      date "+%Y%m%d%H%M%S"

      your copy command is:

      cp sourcefile.txt sourcefile_`date "+%Y%m%d%H%M%S"`.txt

      Or in a loop:

      for f in *.txt
      do
          cp $f `basename $f .txt`_`date "+%Y%m%d%H%M%S"`.txt
      done
      

      Last edited by admin; 1st September 2013 at 01:06 AM. Reason: Code formatting.

  • RunnySpoon Sep 3, 2013 @ 13:37

    Many thanks to Phillipe and cbeerse, that worked a treat.

  • Saurabh Nov 18, 2015 @ 16:29

    Hi,
    I need to get time of 1 hour before. -d and -v options are not working. My requirement is to print current time and 1 hour before time. For current time and 1 hour before time I am using below

    TIME1=`date +"%T"`
    TIME2=`date +"%T"` - 1 * 60 * 60 * 1000

    TIME1 is giving the correct time but TIME2 is not working. Can anyone please respond?

    Thanks,
    Saurabh

  • Sunil Feb 4, 2016 @ 19:53

    I have two variables and would like to find time diff in minutes. Pls help

    TIME1=05:36:11
    TIME2=06:46:00

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.