Get Current date in epoch from Unix shell script

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 10:46:02

问题


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.


回答1:


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))
    



回答2:


The Unix Date command will display in epoch time

the command is

date +"%s"

http://unixhelp.ed.ac.uk/CGI/man-cgi?date

Edit: Some people have observed you asked for days, so it's the result of that command divided by 86,400




回答3:


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



回答4:


echo `date +%s`/86400 | bc



回答5:


Depending on the language you're using it's going to be something simple like

CInt(CDate("1970-1-1") - CDate(Today()))

Ironically enough, yesterday was day 40,000 if you use 1/1/1900 as "day zero" like many computer systems use.



来源:https://stackoverflow.com/questions/1094291/get-current-date-in-epoch-from-unix-shell-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!