Unix Timestamp Code Examples

Complete programming guide with Unix timestamp examples across multiple languages. Learn to work with current timestamps and understand different time units.

Programming Languages

JavaScript

JavaScript handles Unix timestamps natively through the Date object. JavaScript timestamps are in milliseconds by default, which is important to understand when using conversion tools.

Get Current Timestamp

Date.now() // milliseconds

Math.floor(Date.now() / 1000) // seconds

Convert Date to Timestamp

new Date('2022-01-01T00:00:00Z').getTime()

Convert Timestamp to Date

new Date(1640995200000)

new Date(1640995200 * 1000) // from seconds

JavaScript's Date constructor expects milliseconds, so multiply seconds-based timestamps by 1000. Use Date.now() for current time or getTime() to convert Date objects to timestamps.

Python

Python's datetime and time modules provide excellent Unix timestamp support with timezone awareness.

Get Current Timestamp

import time
time.time()

Convert Date to Timestamp

import datetime
datetime.datetime(2022, 1, 1, tzinfo=datetime.timezone.utc).timestamp()

Convert Timestamp to Date

import datetime
datetime.fromtimestamp(1640995200)

Python timestamps are in seconds by default. Always specify timezone information when creating datetime objects to avoid local timezone assumptions. Use time.time() for simple timestamp generation.

Java

Java provides multiple approaches through legacy Date class and modern time API (Java 8+).

Get Current Timestamp

System.currentTimeMillis() / 1000L

Instant.now().getEpochSecond()

Convert Date to Timestamp

LocalDateTime.of(2022, 1, 1, 0, 0).toEpochSecond(ZoneOffset.UTC)

Convert Timestamp to Date

Instant.ofEpochSecond(1640995200L).atZone(ZoneId.systemDefault())

Java's newer time API (java.time) is preferred over legacy Date class. System.currentTimeMillis() returns milliseconds, so divide by 1000 for seconds. Always specify timezone when converting.

C#

C# provides Unix timestamp functionality through DateTime and DateTimeOffset classes, with built-in support since .NET Framework 4.6.

Get Current Timestamp

DateTimeOffset.UtcNow.ToUnixTimeSeconds()

DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()

Convert Date to Timestamp

var date = new DateTimeOffset(2022, 1, 1, 0, 0, 0, TimeSpan.Zero);
date.ToUnixTimeSeconds()

Convert Timestamp to Date

DateTimeOffset.FromUnixTimeSeconds(1640995200)

DateTimeOffset.FromUnixTimeMilliseconds(1640995200000)

C# provides built-in Unix timestamp methods since .NET Framework 4.6 and .NET Core. Use DateTimeOffset for timezone-aware operations. Always work with UTC when dealing with timestamps.

C

C provides basic Unix timestamp functionality through the time.h library.

Get Current Timestamp

#include <time.h>
time_t now = time(NULL);

Convert Date to Timestamp

struct tm tm = {0};
tm.tm_year = 122; // 2022-1900
tm.tm_mon = 0; // January
time_t timestamp = mktime(&tm);

Convert Timestamp to Date

time_t timestamp = 1640995200;
struct tm *tm = localtime(&timestamp);

C handles Unix timestamps as time_t (typically long integer). Remember that tm_year is years since 1900 and tm_mon is 0-indexed. Use gmtime() for UTC, localtime() for local timezone.

C++

C++11 introduced chrono library for modern time handling, while maintaining C compatibility.

Get Current Timestamp

#include <chrono>
auto now = std::chrono::system_clock::now();
auto timestamp = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count();

Convert Date to Timestamp

std::tm tm = {};
tm.tm_year = 122; tm.tm_mon = 0;
std::time_t timestamp = std::mktime(&tm);

Convert Timestamp to Date

auto time_point = std::chrono::system_clock::from_time_t(1640995200);

C++ chrono library provides type-safe time operations. Use duration_cast for precise control over time units. The chrono approach is recommended for new C++ code over C-style time functions.

PHP

PHP offers excellent Unix timestamp support through built-in functions and DateTime class.

Get Current Timestamp

time() // seconds

microtime(true) // with microseconds

Convert Date to Timestamp

strtotime('2022-01-01 00:00:00 UTC')

(new DateTime('2022-01-01T00:00:00Z'))->getTimestamp()

Convert Timestamp to Date

date('Y-m-d H:i:s', 1640995200)

(new DateTime())->setTimestamp(1640995200)->format('Y-m-d H:i:s')

PHP's time() function returns seconds. strtotime() is very flexible for parsing date strings. DateTime class provides object-oriented approach with timezone support. Always specify timezone for consistent results.

Swift

Swift provides modern date handling through Date struct and TimeInterval typealias.

Get Current Timestamp

Date().timeIntervalSince1970

Int(Date().timeIntervalSince1970)

Convert Date to Timestamp

let date = DateFormatter()
date.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let timestamp = date.date(from: "2022-01-01T00:00:00Z")?.timeIntervalSince1970

Convert Timestamp to Date

Date(timeIntervalSince1970: 1640995200)

Date(timeIntervalSince1970: TimeInterval(1640995200))

Swift's Date uses TimeInterval (Double) for timestamps with sub-second precision. timeIntervalSince1970 returns seconds as Double. Use DateFormatter for custom date string parsing and formatting.

Cross-Platform Considerations

When working with Unix timestamps across different programming languages, remember these key points:

  • Unix timestamps represent seconds since January 1, 1970 UTC.
  • Some languages (JavaScript, Java) use milliseconds by default.
  • Always specify timezone when converting dates to avoid local time assumptions.
  • Be aware of integer overflow issues for timestamps beyond 2038 in 32-bit systems.
  • Consider sub-second precision requirements for your application. Use our timestamp generator for testing.

← Back home

Made by Andy Moloney

© unixtime.io