Convert between Unix timestamps and human-readable dates easily
| Start of | Date String | Timestamp |
|---|
How to get current Unix timestamp in different programming languages
| Language | Code Example |
|---|---|
| Python | # Method 1: Using time import time timestamp = int(time.time()) # Method 2: Using datetime from datetime import datetime timestamp = int(datetime.now().timestamp()) |
| JavaScript | // Method 1: Using Date.now() const timestamp = Math.floor(Date.now() / 1000); // Method 2: Using new Date() const timestamp = Math.floor(new Date().getTime() / 1000); |
| Java | // Method 1: Using System long timestamp = System.currentTimeMillis() / 1000L; // Method 2: Using Instant long timestamp = Instant.now().getEpochSecond(); |
| C++ | // Method 1: Using chrono (C++11 and later)
#include <chrono>
auto timestamp = std::chrono::system_clock::now().time_since_epoch() /
std::chrono::seconds(1);
// Method 2: Using time.h
#include <time.h>
time_t timestamp = time(nullptr); |
| C | #include <time.h> time_t timestamp = time(NULL); // or time(0) |
| Go | // Method 1: Unix timestamp timestamp := time.Now().Unix() // Method 2: UnixNano for higher precision timestampNano := time.Now().UnixNano() / 1e9 |
| Rust | // Method 1: Using SystemTime
use std::time::{SystemTime, UNIX_EPOCH};
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
// Method 2: Using time crate
use time::OffsetDateTime;
let timestamp = OffsetDateTime::now_utc().unix_timestamp(); |
| PHP | // Method 1: Using time()
$timestamp = time();
// Method 2: Using strtotime()
$timestamp = strtotime('now');
// Method 3: Using DateTime
$timestamp = (new DateTime())->getTimestamp(); |
| C# | // Method 1: Using DateTimeOffset long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); // Method 2: Using DateTime long timestamp = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeSeconds(); |
| Ruby | # Method 1: Using Time timestamp = Time.now.to_i # Method 2: Using DateTime require 'date' timestamp = DateTime.now.to_time.to_i |
| Swift | // Method 1: Using Date let timestamp = Int(Date().timeIntervalSince1970) // Method 2: Using TimeInterval let timestamp = Int(Date.timeIntervalSinceReferenceDate + Date.timeIntervalBetween1970AndReferenceDate) |
| Kotlin | // Method 1: Using System val timestamp = System.currentTimeMillis() / 1000L // Method 2: Using Instant import java.time.Instant val timestamp = Instant.now().epochSecond |
| R | # Method 1: Using Sys.time() timestamp <- as.integer(Sys.time()) # Method 2: Using as.POSIXct timestamp <- as.integer(as.POSIXct(Sys.time())) |
| MATLAB | % Method 1: Using posixtime
timestamp = posixtime(datetime('now'));
% Method 2: Using now
timestamp = round((now - datenum('1970-01-01')) * 86400); |
| Perl | # Method 1: Using time my $timestamp = time(); # Method 2: Using Time::HiRes for higher precision use Time::HiRes qw(time); my $timestamp = int(time()); |
| Scala | // Method 1: Using System val timestamp = System.currentTimeMillis() / 1000L // Method 2: Using Instant import java.time.Instant val timestamp = Instant.now.getEpochSecond |
| TypeScript | // Method 1: Using Date.now() const timestamp: number = Math.floor(Date.now() / 1000); // Method 2: Using new Date() const timestamp: number = Math.floor(new Date().getTime() / 1000); |
| Dart | // Method 1: Using DateTime int timestamp = DateTime.now().millisecondsSinceEpoch ~/ 1000; // Method 2: Using DateTime.timestamp int timestamp = DateTime.timestamp().millisecondsSinceEpoch ~/ 1000; |
| Lua | -- Method 1: Using os.time()
local timestamp = os.time()
-- Method 2: Using os.date
local timestamp = os.date("*t") |
| SQL | -- MySQL
SELECT UNIX_TIMESTAMP();
-- PostgreSQL
SELECT EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)::INTEGER;
-- SQLite
SELECT strftime('%s', 'now');
-- SQL Server
SELECT DATEDIFF(SECOND, '1970-01-01', GETUTCDATE()); |
| Delphi | // Method 1: Using DateTimeToUnix timestamp := DateTimeToUnix(Now); // Method 2: Using TDateTime uses DateUtils; timestamp := DateTimeToUnix(TDateTime.Now); |
| VB.NET | ' Method 1: Using DateTimeOffset Dim timestamp As Long = DateTimeOffset.UtcNow.ToUnixTimeSeconds() ' Method 2: Using DateTime Dim timestamp As Long = CLng((DateTime.UtcNow - New DateTime(1970, 1, 1)).TotalSeconds) |
| Groovy | // Method 1: Using System def timestamp = System.currentTimeMillis() / 1000L // Method 2: Using Instant import java.time.Instant def timestamp = Instant.now().epochSecond |
| Haskell | -- Method 1: Using getPOSIXTime import Data.Time.Clock.POSIX timestamp <- round <$> getPOSIXTime -- Method 2: Using UTCTime import Data.Time.Clock timestamp <- round . utcTimeToPOSIXSeconds <$> getCurrentTime |