Find out the time since unix epoch for a certain date time?

与世无争的帅哥 提交于 2020-01-01 08:06:39

问题


I want to find out the time in unix time (ie seconds since the unix epoch) on 9:00 BST on 1st October 2009. How can I do this on the linux command line?

I know you can use date @$UNIXTIME '+%someformat', but the unix time is what I'm trying to figure out


回答1:


Using date thus:

date --date="Oct 1 09:00:00 BST 2009" +%s

Yields:

1254384000



回答2:


date +%s

gives seconds since the epoch

Wikipedia (Unix Time) has

To show the time in seconds since 1970-01-01 (Unix epoch):

date +"%s" -d "Fri Apr 24 13:14:39 CDT 2009"

1240596879

I couldn't see your preferred date while I was editing this answer, so I didn't try it out -- but the example I found looks like a similar format.




回答3:


It should work for all cases:

date +%s




回答4:


I use this website: http://www.epochconverter.com/ to convert unixtime to human readable and vice versa. Even though it's not code, it's useful for validating your code. Here's some code (in Java) to do what you asked. The unixtime it prints out is an hour off according to epochconverter.com (not sure why).

SimpleDateFormat sdf = new SimpleDateFormat( "MM/dd/yyyy HH:mm z" );

try {
    Date d = sdf.parse("10/01/2009 09:00 BST");
    System.out.println("Unixtime is: " + d.getTime() / 1000);
} catch (ParseException pe) { pe.printStackTrace(); 


来源:https://stackoverflow.com/questions/1362570/find-out-the-time-since-unix-epoch-for-a-certain-date-time

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