Convert string to date in bash

前端 未结 4 535
挽巷
挽巷 2020-12-08 04:19

I have a string in the format \"yyyymmdd\". It is a string in bash and I want to get it converted into a date so that all other date functions can be used on it.

\"2

4条回答
  •  情话喂你
    2020-12-08 05:04

    We can use date -d option

    1) Change format to "%Y-%m-%d" format i.e 20121212 to 2012-12-12

    date -d '20121212' +'%Y-%m-%d'
    

    2)Get next or last day from a given date=20121212. Like get a date 7 days in past with specific format

    date -d '20121212 -7 days' +'%Y-%m-%d'
    

    3) If we are getting date in some variable say dat

    dat2=$(date -d "$dat -1 days" +'%Y%m%d')
    

提交回复
热议问题