How to get the epoch timestamp?

How to get the epoch timestamp, the number of seconds passed since the epoch?

asked Mar 31 by zma (2,200 points)
retagged Apr 6 by zma

7 Answers

In Python:

You can get the epoch time by calling time.time():

import time

print time.time()
answered Apr 1 by zma (2,200 points)

In Bash, you can call the date command with format option "+%s":

date +%s

Here, "%s" means:


seconds since 1970-01-01 00:00:00 UTC
answered Apr 1 by zma (2,200 points)

In Go, you can use the time.Now() func to get current time and its Unix() func to convert it into an epoch timestamp:


func getEpochTime() int64 {
    return time.Now().Unix()
answered Apr 4 by zma (2,200 points)

In PHP, you can use the time() function.

ts = time();

To get the microsecond together too as a float number, use the microtime(true) function.

ts2 = microtime(true);


$ php -a
Interactive shell

php > echo time() . PHP_EOL;
php > echo microtime(true) . PHP_EOL;
php > 
answered Apr 6 by zma (2,200 points)

In Perl, to get the epoch time, call the time function:

my $ts = time


$ perl -e 'print time . "\n"'
answered Apr 6 by zma (2,200 points)

In Java, you can get the current time in milliseconds and then calculate the epoch time:

long ts = System.currentTimeMillis()/1000;


$ wget --quiet -O /tmp/javarepo-428.jar && java -jar /tmp/javarepo-428.jar
Welcome to JavaREPL version 428 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_25)
Type expression to evaluate, :help for more options or press tab to auto-complete.
Connected to local instance at http://localhost:33598
java> long ts = System.currentTimeMillis()/1000;
long ts = 1491474800
answered Apr 6 by zma (2,200 points)

In C, from man 7 time:

UNIX systems represent time in seconds since the Epoch, 1970-01-01
00:00:00 +0000 (UTC).

A program can determine the calendar time using gettimeofday(2), which
returns time (in seconds and microseconds) that have elapsed since the
Epoch; time(2) provides similar information, but only with accuracy to
the nearest second.

You can use the time() library function to get the epoch timestamp:

On 32-bit POSIX systems:

fprintf(stdout, "%u\n", (unsigned)time(NULL)); 

On 64-bit POSIX systems:

fprintf(stdout, "%lu\n", (unsigned long)time(NULL)); 
answered Apr 8 by zma (2,200 points)

Please log in or register to answer this question.

Related questions

4 answers
5 answers
0 answers
8 answers
asked Apr 6 by anonymous
Welcome to Do This In Various Langs (dtivl), where you can ask questions and receive solutions in various programming languages.
Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost