Understanding Epoch Time
Epoch time, also known as Unix time or POSIX time, is a system for tracking time that sets the “epoch” at January 1, 1970, UTC. It represents the number of seconds that have elapsed since this point in time. This system is widely used in computing for timekeeping purposes.
Converting Epoch Time in Various Programming Languages
JavaScript
// Convert epoch time to human-readable date
let epochTime = 1609459200;
let date = new Date(epochTime * 1000);
console.log(date.toUTCString());
// Convert human-readable date to epoch time
let dateObject = new Date('2021-01-01T00:00:00Z');
let epochTimeFromDate = Math.floor(dateObject.getTime() / 1000);
console.log(epochTimeFromDate);
Python
import time
# Convert epoch time to human-readable date
epoch_time = 1609459200
human_readable_date = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(epoch_time))
print(human_readable_date)
# Convert human-readable date to epoch time
date_str = '2021-01-01 00:00:00'
pattern = '%Y-%m-%d %H:%M:%S'
epoch_time_from_date = int(time.mktime(time.strptime(date_str, pattern)))
print(epoch_time_from_date)
Java
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
// Convert epoch time to human-readable date
long epochTime = 1609459200;
LocalDateTime date =
LocalDateTime.ofInstant(Instant.ofEpochSecond(epochTime), ZoneId.of("UTC"));
System.out.println(date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
// Convert human-readable date to epoch time
String dateStr = "2021-01-01 00:00:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.parse(dateStr, formatter);
long epochTimeFromDate = localDateTime.atZone(ZoneId.of("UTC")).toEpochSecond();
System.out.println(epochTimeFromDate);
Practical Application with a Simple Web App
Let’s create a simple web app that converts between human-readable dates and epoch time using JavaScript.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Epoch Time Converter</title>
<style>
body { font-family: Arial, sans-serif; }
.container { max-width: 600px; margin: 0 auto; padding: 20px; }
input { width: 100%; padding: 10px; margin: 10px 0; }
button { padding: 10px; background-color: #4CAF50; color: white; border: none; cursor: pointer; }
</style>
</head>
<body>
<div class="container">
<h1>Epoch Time Converter</h1>
<label for="epochTime">Epoch Time</label>
<input type="text" id="epochTime" placeholder="Enter epoch time">
<button onclick="convertToLocalTime()">Convert to Human-Readable Date</button>
<p id="localTime"></p>
<hr>
<label for="dateTime">Human-Readable Date (YYYY-MM-DD HH:MM:SS)</label>
<input type="text" id="dateTime" placeholder="Enter date time">
<button onclick="convertToEpochTime()">Convert to Epoch Time</button>
<p id="epochTimeDisplay"></p>
</div>
<script>
function convertToLocalTime() {
let epochTime = document.getElementById('epochTime').value;
let date = new Date(epochTime * 1000);
document.getElementById('localTime').innerText = date.toUTCString();
}
function convertToEpochTime() {
let dateTime = document.getElementById('dateTime').value;
let date = new Date(dateTime);
let epochTime = Math.floor(date.getTime() / 1000);
document.getElementById('epochTimeDisplay').innerText = epochTime;
}
</script>
</body>
</html>
Understanding and utilizing epoch time can greatly simplify time and date manipulations in various programming contexts. The provided code snippets and the web app example offer a comprehensive guide to effectively harness this essential concept.
Hash: d773f36aafe0ece4c472f4d1ed8d0ea4ac343d8c2f54059424f269df17c6cbb5