Unix Timestamp Converter
Timestamp to DateTime
DateTime to Timestamp
Hour
Minute
Second
Code Examples in Different Languages
Python
import time
from datetime import datetime
# Get current timestamp
current_timestamp = int(time.time())
print(f"Current timestamp: {current_timestamp}")
# Convert timestamp to datetime
timestamp = 1640995200 # 2022-01-01 00:00:00
dt = datetime.fromtimestamp(timestamp)
formatted_time = dt.strftime('%Y-%m-%d %H:%M:%S')
print(f"Formatted time: {formatted_time}")
# Convert datetime to timestamp
dt = datetime(2022, 1, 1, 0, 0, 0)
timestamp = int(dt.timestamp())
print(f"Converted timestamp: {timestamp}")
JavaScript
// Get current timestamp
const currentTimestamp = Math.floor(Date.now() / 1000);
console.log(`Current timestamp: ${currentTimestamp}`);
// Convert timestamp to datetime
const timestamp = 1640995200; // 2022-01-01 00:00:00
const date = new Date(timestamp * 1000);
const formattedTime = date.toLocaleString();
console.log(`Formatted time: ${formattedTime}`);
// Convert datetime to timestamp
const dateStr = '2022-01-01 00:00:00';
const timestamp2 = Math.floor(new Date(dateStr).getTime() / 1000);
console.log(`Converted timestamp: ${timestamp2}`);
PHP
<?php
// Get current timestamp
$current_timestamp = time();
echo "Current timestamp: " . $current_timestamp . "\n";
// Convert timestamp to datetime
$timestamp = 1640995200; // 2022-01-01 00:00:00
$formatted_time = date('Y-m-d H:i:s', $timestamp);
echo "Formatted time: " . $formatted_time . "\n";
// Convert datetime to timestamp
$date = '2022-01-01 00:00:00';
$timestamp = strtotime($date);
echo "Converted timestamp: " . $timestamp . "\n";
Java
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
public class TimeConverter {
public static void main(String[] args) {
// Get current timestamp
long currentTimestamp = System.currentTimeMillis() / 1000L;
System.out.println("Current timestamp: " + currentTimestamp);
// Convert timestamp to datetime
long timestamp = 1640995200L; // 2022-01-01 00:00:00
LocalDateTime dateTime = LocalDateTime.ofInstant(
Instant.ofEpochSecond(timestamp),
ZoneId.systemDefault()
);
String formattedTime = dateTime.format(
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
);
System.out.println("Formatted time: " + formattedTime);
// Convert datetime to timestamp
LocalDateTime dt = LocalDateTime.of(2022, 1, 1, 0, 0, 0);
long newTimestamp = dt.atZone(ZoneId.systemDefault())
.toEpochSecond();
System.out.println("Converted timestamp: " + newTimestamp);
}
}
Go
package main
import (
"fmt"
"time"
)
func main() {
// Get current timestamp
currentTimestamp := time.Now().Unix()
fmt.Printf("Current timestamp: %d\n", currentTimestamp)
// Convert timestamp to datetime
timestamp := int64(1640995200) // 2022-01-01 00:00:00
tm := time.Unix(timestamp, 0)
formattedTime := tm.Format("2006-01-02 15:04:05")
fmt.Printf("Formatted time: %s\n", formattedTime)
// Convert datetime to timestamp
t, _ := time.Parse("2006-01-02 15:04:05", "2022-01-01 00:00:00")
newTimestamp := t.Unix()
fmt.Printf("Converted timestamp: %d\n", newTimestamp)
}
What is Unix Timestamp?
Unix Timestamp is an integer value used to represent time, indicating the number of seconds that have elapsed since UTC 00:00:00 on January 1, 1970 (also known as the Unix epoch). This time representation is widely used in computer systems and programming.
Why Use Timestamp?
Timestamps have the following advantages:
- Simple: Represents a point in time with a single integer
- Unified: Not affected by time zones, making it easy to use globally
- Easy Calculation: Can be directly compared and calculated
- Storage Efficiency: Only requires storing a single integer
Timestamp Use Cases
Timestamps are commonly used in the following scenarios:
- Creation and modification time of database records
- Time recording in logging systems
- Modification time of files
- Calculating time intervals
- Time synchronization across time zones
Considerations
When using Unix Timestamp, it is important to note:
- 32-bit systems will experience a timestamp overflow on January 19, 2038, at 03:14:07 UTC (known as the Year 2038 problem)
- To avoid this issue, it is recommended to use a 64-bit timestamp
- Different programming languages may use different time precisions (seconds, milliseconds, microseconds, etc.)