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. The date command shows the date and time read from the kernel clock.

UNIX Date Command Syntax

The syntax is:

date 
date "+format"

date
date "+format"

Task: Display Current Date and Time

Type the following command:

date

date

Sample outputs:

Tue Oct 27 15:35:08 CDT 2009

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

Task: Set The Current Time

To set the current time to 05:30:30, enter:

date 0530.30

date 0530.30

Task: Set Date

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

date 10250045

date 10250045

Another example – set the current date and time to Oct 15, 2009 04:30 you type:

date --set="20091015 04:30"

date –set="20091015 04:30"

Task: 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'

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'

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

Unix Command Help

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

man date

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.