# Epoch time in Perl

PERLServer Side ProgrammingProgramming Scripts

#### Perl Online Training

46 Lectures 4.5 hours

#### COMPLETE PERL Programming

11 Lectures 1.5 hours

#### Perl for Beginners: Learn A to Z of Perl Scripting Hands-on

30 Lectures 6 hours

You can use the time() function in Perl to get epoch time, i.e., the numbers of seconds that have elapsed since a given date, in Unix is January 1, 1970.

## Example

Live Demo

#!/usr/local/bin/perl
$epoc = time(); print "Number of seconds since Jan 1, 1970 -$epoc";

## Output

When the above code is executed, it produces the following result −

Number of seconds since Jan 1, 1970 - 1361022130

You can convert a given number of seconds into date and time string as follows −

## Example

Live Demo

#!/usr/local/bin/perl
$datestring = localtime(); print "Current date and time$datestring";
$epoc = time();$epoc = $epoc - 24 * 60 * 60; # one day before of current date.$datestring = localtime($epoc); print "Yesterday's date and time$datestring";

## Output

When the above code is executed, it produces the following result −

Current date and time Tue Jun 5 05:54:43 2018
Yesterday's date and time Mon Jun 4 05:54:43 2018
Updated on 29-Nov-2019 06:33:42