Linux provides commands that allow you to view system’s time information, and with these commands, you can quickly get the time related information.

System Time

We can use the date command to get or set the time of the system:

1
date
img

Outputs system's datetime

Timezone

We can use the TZ variable to specify the time zone where our server is located:

1
2
export TZ=Pacific/Auckland
date
img

Changes timezone to Pacific Auckland

Time in Difference Timezone

Shows the time in Shanghai when a Auckland clock shows 8:07 on March 07, 2019:

1
2
export TZ="Asia/Shanghai"
date --date='TZ="Pacific/Auckland" 2019-03-07 08:07'
img

Changes timezone to China Shanghai and shows the corresponding time in Shanghai

Hour - 24-hour format

1
date +'%H'
img

Outputs current hour in 24 hours format

Hour - 24-hour space padded format

1
date +'%k'
img

Outputs current hour in 24 hours space padded format

Hour - 12-hour format

1
date +'%I'
img

Outputs current hour in 12 hours format

Hour - 12-hour space padded format

1
date +'%l'
img

Outputs current hour in 12 hours space padded format

Minute

1
date +'%M'
img

Outputs current minutes

Second

1
date +'%S'
img

Outputs current seconds

Nanosecond

1
date +'%N'
img

Outputs current nanoseconds

Seconds Since Epoch (Timestamp)

1
date +'s'
img

Outputs current timestamp

Time - 24-hour format (Hour:Minute:Second)

1
date +'%T'
img

Outputs hour, minute, second in 24-hour time

Time - 24-hour format (Hour:Minute)

1
date +'%R'
img

Outputs hour, minute in 24-hour time

Locale Time - 24-hour Clock Time

1
date +'%X'
img

Outputs 24-hour clock time

Locale Time - 12-hour Clock Time

1
date +'%r'
img

Outputs 12-hour clock time

AM or PM - Uppercase

1
date +'%p'
img

Outputs a.m. or p.m. in uppercase

AM or PM - Lowercase

1
date +'%P'
img

Outputs a.m. or p.m. in lowercase

Timezone - Four Digits

1
date +'%z'
img

Outputs four digits timezone

Timezone - Nearest Minute

1
date +'%:z'
img

Outputs timezone in nearest minute

Timezone - Nearest Second

1
date +'%::z'
img

Outputs timezone in nearest second

Timezone - Minimum Necessary Precision

1
date +'%:::z'
img

Outputs the minimum necessary part of the timezone

Timezone - Alphabetic Abbreviation

1
date +'%Z'
img

Outputs the timezone in alphabetic abbreviation

References 21 System context, 21.1 date: Print or set system date and time, TWiki.org Service: Date and Time Gateway, 29.10 Specifying time zone rules

Buy me a coffeeBuy me a coffee