Different Date/Time format in shell

Other topics

Remarks:

Below are a few useful links for the date command in Unix shells:

Sample Code & output

#!/bin/bash

#Print Date / Time in different Formats
date1=$(date +'%d-%m-%y')
date2=$(date +'%d-%m-%Y')
date3=$(date +'%d-%b-%Y')
date4=$(date +'%d-%B-%Y')
date5=$(date +'%a %d-%b-%Y')
date6=$(date +'%a %d-%b-%Y  %Z')
date7=$(date +'%A %d-%b-%Y')

echo "Print Date in different format"
echo $date1
echo $date2
echo $date3
echo $date4
echo $date5
echo $date6
echo $date7
echo

#print Timestamp
time1=$(date '+%H:%M:%S')
time2=$(date '+%I:%M:%S')
time3=$(date '+%r')
time4=$(date '+%R')

echo "Print Time in different format"
echo "Time in 24h clock: $time1"
echo "Time in 12h clock: $time2"
echo "Time with AM/PM: $time3"
echo "Time in hour&minute: $time4"

exit

Output

Print Date in different format
01-08-16
01-08-2016
01-Aug-2016
01-August-2016
Mon 01-Aug-2016
Mon 01-Aug-2016 IST
Monday 01-Aug-2016

Print Time in different format
Time in 24h clock: 15:16:06
Time in 12h clock: 03:16:06
Time with AM/PM: 03:16:06 PM
Time in hour&minute: 15:16

Parameters:

FormatInterpreted as
%%literal percent sign (%)
%Aweekday name (e.g. Sunday)
%aweekday name in short fomat (e.g. Sun)
%Bfull month name (e.g. January)
%bmonth name (e.g. Jan)
%Hhour (00..23)
%Ihour (01..12)
%jday of year (001..366)
%khour ( 0..23)
%lhour ( 1..12)
%Mminute (00..59)
%mmonth (01..12)
%pDefine AM or PM; blank if not known
%R24-hour hour and minute; same as %H:%M
%r12-hour clock time (e.g. 11:11:04 PM)
%Ssecond (00..60)
%sUnix epoch: seconds since 1970-01-01 00:00:00 UTC (not available in older UNIXes)
%Ttime, equivalent to %H:%M:%S
%Ztime zone name (e.g. PDT)
%ztime zone offset (direction, hours, minutes, e.g. -0700)

Contributors

Topic Id: 4850

Example Ids: 17094

This site is not affiliated with any of the contributors.