≡ Menu

date command

Let us say you need to display time for different zones or cities. Sure you can use The World Clock - Time Zones located here. If you need a different time zone at a shell prompt or script use old good date command.

Different timezones can be displayed by changing the TZ environment variable.

TZ Specifies the timezone, unless overridden by command line parameters. If neither is specified, the setting from /etc/localtime is used by Linux/UNIX computer.

Display current date and timezone

$ date

Tue Jul 24 22:05:54 CDT 2007

Display time of different timezones using the date command and TZ variable

Set timezone to PDT:
$ export TZ=America/Los_Angeles
$ date


Tue Jul 24 20:12:01 PDT 2007

Or set timezone to IST:
$ export TZ=Asia/Calcutta
$ date


Wed Jul 25 08:46:12 IST 2007

Remove the TZ variable with unset command:
$ unset TZ
$ date


Tue Jul 24 22:17:31 CDT 2007

Read date,bash,unset command man page for more information. Hope this small tip will save your time and see a different time zone.

Related: Howto: Redhat enterprise Linux / CentOS setup DST - Daylight Saving Time

When invoked without arguments, the date command displays the current date and time. Depending on the options specified, date will set the date and time or print it in a user defined way. I've seen many sysadmin writing perl scripts for calculating relative date such as yesterdays or tomorrows day. You can use GNU date command, which is designed to handle relative date calculation such as:

  • 1 Year
  • 2 Days
  • 2 Days ago
  • 5 Years

[click to continue…]

FreeBSD use the Network Time Protocol (NTP) for synchronizing the clocks of computer systems over packet-switched, variable-latency data networks. NTP uses UDP port 123. If you have one computer or single server then you can easily synchronization time with other NTP servers. All you need is ntp client called ntpdate. It is use to set the date and time via NTP servers.

FreeBSD: Install NTP Client

Use any one of the following command to install NTP:

# pkg_add -rv  ntp


# cd /usr/ports/net/ntp
# make; make install

Pick appropriate NTP Servers

Visit public ntp timeserver list to pick up your NTP server.

Open UDP port 123 at firewall

If you are running FreeBSD ipfilter firewall, you need to open the UDP port 123. Just add following rule to your firewall script:

pass out quick on lnc0 proto udp from YOUR-SERVER to any port = 123
keep state


pass out quick on lnc0 proto udp from YOUR-SERVER to
TIME-SERVER-IP port = 123 keep state

For example, my FreeBSD workstation IP is and is IP of NTP server then my rule is in ipf.conf file as follows:

pass out quick on lnc0 proto udp from
to port = 123 keep state

FreeBSD test clock synchronization

Just run ntpdate command as follows to see you can set date and clock via NTP:
Set wrong date (Mon Dec 13 4:27 pm):

# date 0412131627

Now set correct date with ntp client:

# ntpdate -v -b in.pool.ntp.org

13 Dec 16:27:50 ntpdate[997]: ntpdate 4.2.0-a Thu Nov 3 07:34:22 UTC 2005 (1)
25 Jan 12:35:47 ntpdate[997]: step time server offset 35237275.965726 sec

You can verify that correct data is setup:

# date


Wed Jan 25 12:36:21 IST 2006

Enable date and time/ clock Synchronization at boot time

You need to set ntpdate via /etc/rc.local file.

# vi /etc/rc.conf

Append following line to it:

Save and close the file. Make sure you have correct ntpdate_hosts server entry.

See also:

Updated for accuracy.