Linux date command format with examples

 
To get the current date and time in Linux, command to be used is date.

% date

will display as

Wed Nov 17 09:32:36 PST 2016

Here the time stamp is PST , Pacific Standard Time.

In order to change the time zone, environment variable named TZ needs to be used. It stand for Time Zone. Various time zones are supported by Linux. As per our convenient, we can set the TZ variable.

Lets set the TZ with IST, Indian Standard Time zone as shown below

Variable needs to be set with "Asia/Kolkata" to see the time in IST.

In the bash environment, it can be set as,

export TZ="Asia/Kolkata"

In case of CSH environment, it can be set as,

setenv TZ "Asia/Kolkata"

Now, if you try with date command on Linux terminal or shell, it will get displayed in Indian Standard Time zone.

% date

will display,

Wed Nov 17 11:02:45 IST 2016

Note the change from PST to IST.

How to display date in different formats?

In order to display the date in dd-mm-yyyy format,

% date +"%d-%m-%Y"

To get it displayed in yyyy-mm-dd format,

% date +"%Y-%m-%d"

Capital  alphabet "Y" is used to display the year in 4 digits, if you want to get the year displayed in 2 digits it can be replaced by small alphabet "y".

How to display the time stamp only from the date command?

%date +"%T"

It will display only the time. Note that the time will be in 24 hours format.