Get Current date in epoch from Unix shell script

前端 未结 5 599
忘掉有多难
忘掉有多难 2020-12-23 12:53

How to get the current date value in epoch i.e., number of days elapsed since 1970-1-1. I need solution in unix shell script.

5条回答
  •  遥遥无期
    2020-12-23 13:53

    Update: The answer previously posted here linked to a custom script that is no longer available, solely because the OP indicated that date +'%s' didn't work for him. Please see UberAlex' answer and cadrian's answer for proper solutions. In short:

    1. For the number of seconds since the Unix epoch use date(1) as follows:

      date +'%s'
      
    2. For the number of days since the Unix epoch divide the result by the number of seconds in a day (mind the double parentheses!):

      echo $(($(date +%s) / 60 / 60 / 24))
      

提交回复
热议问题