Amandasaurus
Amandasaurus

Reputation: 60719

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

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

Upvotes: 15

Views: 36783

Answers (4)

Hafiz Muhammad Shafiq
Hafiz Muhammad Shafiq

Reputation: 8680

It should work for all cases:

date +%s

Upvotes: 5

Vik David
Vik David

Reputation: 3750

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

Upvotes: -1

pavium
pavium

Reputation: 15118

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.

Upvotes: 11

Adam Bellaire
Adam Bellaire

Reputation: 110499

Using date thus:

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

Yields:

1254384000

Upvotes: 32

Related Questions