Search code examples
bashshellunixunix-timestamp

timestamp variable in unix


I am getting below data in in bash script variable exp_dt:

exho $exp_dt
Jun  5 08:25:42 2023 GMT

need help to convert it into format like "05-Jun-2023" and days remaining from current date i.e. 64 days.

expected output

echo $new_var
05-Jun-2023|64 days

Solution

  • try this:

    # your date
    exp_dt="Jun  5 08:25:42 2023 GMT"
    
    # actual day of year
    b=$(date "+%-j")
    
    # day of year of your date
    c=$(date -d "$exp_dt" "+%-j")
    
    # diffence of days
    new_var=$((c-b))
    
    # your solution
    echo $(date -d "$exp_dt" "+%d-%m-%Y") "| $new_var days"