shell Different Date/Time format in shell Sample Code & output


Example

#!/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