HollowBastion HollowBastion - 5 days ago 6
Bash Question

Convert date string to a different format in bash

Getting the date in bash works something like this:

now=$(date)
echo "Current date: $now"


It returns:

Current date: Mon Nov 28 11:34:55 NZDT 2016


I need to store the date in a variable where the date is in a format like this:

20161128


How can I convert the date format into the format I need?

Answer

You use any format with +%

date +%Y%m%d

Output

20161127
Comments