Timestamp Code Snippets

Code examples for getting, converting timestamps in various programming languages

Get Current Unix Timestamp

How to get the current Unix timestamp in different programming languages

Language Code Action
PHP
<?php
echo time(); // Get current Unix timestamp
// or
echo strtotime("now");
JavaScript
// Method 1: Millisecond timestamp, divide by 1000 to get seconds
Math.floor(Date.now() / 1000);

// Method 2: Second timestamp
new Date().getTime() / 1000;
Python
import time

# Second timestamp
int(time.time())

# Millisecond timestamp
int(time.time() * 1000)
Java
import java.time.Instant;

// Second timestamp
long timestamp = Instant.now().getEpochSecond();

// Millisecond timestamp
long timestampMillis = Instant.now().toEpochMilli();
C#
using System;

// Unix timestamp (seconds)
long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();

// Unix timestamp (milliseconds)
long timestampMillis = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
Go
package main

import (
    "fmt"
    "time"
)

func main() {
    // Second timestamp
    fmt.Println(time.Now().Unix())

    // Nanosecond timestamp
    fmt.Println(time.Now().UnixNano())
}
Ruby
# Second timestamp
Time.now.to_i

# Millisecond timestamp
(Time.now.to_f * 1000).to_i
Node.js
// Second timestamp
Math.floor(Date.now() / 1000);

// Millisecond timestamp
Date.now();
Bash/Shell
# Second timestamp
date +%s

# Millisecond timestamp
date +%s%3N
Swift
import Foundation

// Second timestamp
let timestamp = Int(Date().timeIntervalSince1970)

// Millisecond timestamp
let timestampMillis = Int(Date().timeIntervalSince1970 * 1000)

Convert Unix Timestamp to Date

How to convert Unix timestamp to regular date/time in different programming languages

Language Code Action
PHP
<?php
$timestamp = 1704067200;

// Convert to date time
echo date("Y-m-d H:i:s", $timestamp);
// Output: 2024-01-01 00:00:00

// Custom format
echo date("Y年m月d日 H时i分s秒", $timestamp);
// Output: 2024年01月01日 00时00分00秒
JavaScript
const timestamp = 1704067200; // Second timestamp

// Convert millisecond timestamp
const date = new Date(timestamp * 1000);

// Format date time
console.log(date.toLocaleString());
// Output: 2024/1/1 00:00:00

// Custom format
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
const hours = String(date.getHours()).padStart(2, "0");
const minutes = String(date.getMinutes()).padStart(2, "0");
const seconds = String(date.getSeconds()).padStart(2, "0");
console.log(`-- ::`);
Python
import time
from datetime import datetime

timestamp = 1704067200

# Method 1: Use time module
print(time.ctime(timestamp))
# Output: Mon Jan  1 00:00:00 2024

# Method 2: Use datetime module
dt = datetime.fromtimestamp(timestamp)
print(dt.strftime("%Y-%m-%d %H:%M:%S"))
# Output: 2024-01-01 00:00:00
Java
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

long timestamp = 1704067200L;

// Convert to DateTime
Instant instant = Instant.ofEpochSecond(timestamp);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
    .withZone(ZoneId.systemDefault());

// Format output
String formattedDate = formatter.format(instant);
System.out.println(formattedDate);
C#
using System;

long timestamp = 1704067200L;

// Convert to DateTime
DateTime dateTime = DateTimeOffset.FromUnixTimeSeconds(timestamp).DateTime;

// Format output
Console.WriteLine(dateTime.ToString("yyyy-MM-dd HH:mm:ss"));
// Output: 2024-01-01 00:00:00
Go
package main

import (
    "fmt"
    "time"
)

func main() {
    timestamp := int64(1704067200)

    // Convert to time.Time
    t := time.Unix(timestamp, 0)

    // Format output
    fmt.Println(t.Format("2006-01-02 15:04:05"))
    // Output: 2024-01-01 00:00:00
}
Ruby
timestamp = 1704067200

# Convert to Time object
time = Time.at(timestamp)

# Format output
puts time.strftime("%Y-%m-%d %H:%M:%S")
# Output: 2024-01-01 00:00:00
Bash/Shell
timestamp=1704067200

# Convert to readable date
date -d @$timestamp "+%Y-%m-%d %H:%M:%S"
# Output: 2024-01-01 00:00:00

Convert Date to Unix Timestamp

How to convert regular date/time to Unix timestamp in different programming languages

Language Code Action
PHP
<?php
// Method 1: Use strtotime
$timestamp1 = strtotime("2024-01-01 00:00:00");

// Method 2: Use mktime
$timestamp2 = mktime(0, 0, 0, 1, 1, 2024);

echo $timestamp1; // Output: 1704067200
JavaScript
// Create date object
const date = new Date("2024-01-01 00:00:00");

// Get second timestamp
const timestamp = Math.floor(date.getTime() / 1000);

console.log(timestamp); // Output: 1704067200
Python
import time
from datetime import datetime

# Method 1: Use time module
timestamp1 = int(time.mktime(datetime(2024, 1, 1, 0, 0, 0).timetuple()))

# Method 2: Use datetime module
dt = datetime(2024, 1, 1, 0, 0, 0)
timestamp2 = int(dt.timestamp())

print(timestamp1) # Output: 1704067200
Java
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

// Create LocalDateTime
LocalDateTime localDateTime = LocalDateTime.of(2024, 1, 1, 0, 0, 0);

// Convert to Instant (use system timezone)
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();

// Get Unix timestamp
long timestamp = instant.getEpochSecond();
System.out.println(timestamp);
C#
using System;

// Create DateTime
DateTime dateTime = new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc);

// Convert to Unix timestamp
long timestamp = new DateTimeOffset(dateTime).ToUnixTimeSeconds();

Console.WriteLine(timestamp);
Go
package main

import (
    "fmt"
    "time"
)

func main() {
    // Create time object
    t := time.Date(2024, 1, 1, 0, 0, 0, 0, time.Local)

    // Get Unix timestamp
    timestamp := t.Unix()

    fmt.Println(timestamp) // Output: 1704067200
}
Ruby
# Create Time object
time = Time.new(2024, 1, 1, 0, 0, 0)

# Get Unix timestamp
timestamp = time.to_i

puts timestamp # Output: 1704067200
Bash/Shell
# Convert to Unix timestamp
date -d "2024-01-01 00:00:00" +%s

# Output: 1704067200