Date and hwclock Commands with Example

Date Command


Date command is used to display the current date and time or setting the system date and time on terminal or through ssh session.


date command Examples


To display the current date and time use date command

$ date

Thu Aug 28 17:50:29 IST 2014



To Display The Hardware Clock (RTC), you need to be root user to use hwclock command.

#hwclock

Thu 28 Aug 2014 05:56:45 PM IST -0.021472 seconds


Difference in Hardware and System clock


There are two clocks. One is RTC or CMOS Clock / Hardware Clock, A computer has a battery driven hardware clock. The battery ensures that the clock will work even if the rest of the computer is without electricity. Other is the System Clock or Kernel Clock, Software Clock whose value is saved in the OS config. This system clock is based on Timer Interrupts.

The Hardware-Clock is generated on the hardware. That guarantees a stable, exact and fast clock rate. The Software-Clock is generated in the PC. This is a stable and exact too, but normally limited at 1000 Hz.


Converting a date in date format

$ date --date="2/8/2014"

Sat Feb 8 00:00:00 IST 2014

$ date --date="8 feb 14"

Sat Feb 8 00:00:00 IST 2014



Displaying relative date

$ date --date="yesterday"

Wed Aug 27 18:17:54 IST 2014

$ date --date="tomorrow"

Fri Aug 29 18:18:04 IST 2014

$ date --date="next day"

Fri Aug 29 18:18:18 IST 2014

$ date --date="next monday"

Mon Sep 1 00:00:00 IST 2014

$ date --date="10 second ago"

Thu Aug 28 18:19:08 IST 2014

$ date --date="1 month ago"

Mon Jul 28 18:20:44 IST 2014



Setting date and time

Use –set option or –s option as below to set date and time using root user

# date --set="28 AUG 2014 18:00:00"

# date -s "28 AUG 2014 18:00:00"


Or use specified format to set date and time

# date +%Y%m%d -s "20140828"

# date +%T -s "10:13:13"

Here date is in yyyymmdd and time in HH:MM:SS


Changing the date and time to 2014-08-28 23:57 you can use the command without any option. Here date format is mmddHHMMyyyy

# date 082823572014

or it can be done using these two commands with –set or –s option

date -set 2014-08-28
date -set 23:57:00



hwclock command


Use hwclock command to view hardware date and time

# hwclock

There may be some time difference between system clock and hardware clock, you can synchronize these two clock using below methods.

Setting Hardware clock to current System time

# hwclock –systohc

–systohc stands for “system to hardware clock”, which copies the time from system to hardware clock.

or

# hwclock –w


Setting system clock with hardware clock timing

# hwclock –hctosys

or

# hwclock –s


Setting hardware clock and time

# hwclock --set --date "8/11/2013 23:10:45"




No comments: