What is date/time format? and how to set date variable in shell script?

Can any one help me understand the date and time format and setting date as variable in shell script ?Help me to understand below one:

date=`date +"%Y-%m-%d"`;
date1=`date +"%Y%m%d"`;
CurrentHour=`date +"%H"`;
Previous=`expr $(date +"%H") - 1`;

Any help ?

date=`date +"%Y-%m-%d"`;

This will give you the current date in this format YYYY-MM-DD.


date1=`date +"%Y%m%d"`;

This will give you the current date also but in this format YYYYMMDD *notice there are no dashes.


CurrentHour=`date +"%H"`;

As mentioned in the variable name, this will give the current hour (24-hour type)


Previous=`expr $(date +"%H") - 1`;

This will give you the current hour minus 1, for example if the current hour is 5, this will give you 4.


Check this out for more date variables:

2 Likes

Thank you ,able to understand clearly ,if possible let me know if any other possibilities.

You can get (year-month-day) from date as follows:

DATE=date +%Y-%m-%d

Then get (year-month-day hour:minute:second) from date as follows:

DATE=date +%Y-%m-%d:%H:%M:%S

You can also get more options from here

There are unlimited number of possibilities, you can do any combinations you want with all the formats available, please check the table from the link I’ve provided.

Good luck :smile: