Format Date For Display or Use In a Shell or Bash Script
date command displays current Date and Time in Shell/Bash Script.We need to use the date command with specific format to format date or time in Linux or Unix shell scripts.
This post shows you how to format date in Linux or Unix-based system.
Different usage with Output:
To get current data with Timestamp and time in Unix/Linux/Bash/Shell scripts.
date
Sun Mar 29 02:10:38 EDT 2015
date +%Y%m%d%H%M%S
20150329021052
date “+%Y-%m-%d %H:%M:%S.%N”
2015-03-29 02:11:12.784721000
Bash Date Formats:
- Y= Year in YYYY format
- m=Month in MM format
- y=Year in YY format
- d=day in DD format
- H=Hour in HH format
- M=Minute in MM format
- S=Seconds in SS format
- N=Nano seconds in NNNNNNNN format
Subtracting dates with output:
date -d”1 day ago“ — for taking yesterdays date and time.
Sat Mar 28 02:15:32 EDT 2015
(current date is “Sun Mar 29 02:15:32 EDT 2015 “)
date -d”1 hour ago“ — for taking 1hour ago time
Sun Mar 29 01:17:40 EDT 2015