// Current Unix timestamp (seconds)
const now = Math.floor(Date.now() / 1000);
// Current timestamp in milliseconds
const ms = Date.now();
// Convert timestamp to Date object
const date = new Date(1710492223 * 1000);
// Format to ISO 8601 string
date.toISOString(); // "2024-03-15T12:23:43.000Z"
// Format to locale string
date.toLocaleString("en-US", {
timeZone: "America/New_York",
dateStyle: "full",
timeStyle: "long",
});
// Parse a date string to timestamp
const ts = new Date("2024-03-15T08:00:00Z").getTime() / 1000;
// Add 7 days to a timestamp
const nextWeek = now + 7 * 24 * 60 * 60;
// Compare two timestamps
const isExpired = now > expiresAt;
// Get individual components
const year = date.getFullYear();
const month = date.getMonth() + 1; // 0-indexed
const day = date.getDate();
const hours = date.getHours();
// Current Unix timestamp (seconds)
const now = Math.floor(Date.now() / 1000);
// Current timestamp in milliseconds
const ms = Date.now();
// Convert timestamp to Date object
const date = new Date(1710492223 * 1000);
// Format to ISO 8601 string
date.toISOString(); // "2024-03-15T12:23:43.000Z"
// Format to locale string
date.toLocaleString("en-US", {
timeZone: "America/New_York",
dateStyle: "full",
timeStyle: "long",
});
// Parse a date string to timestamp
const ts = new Date("2024-03-15T08:00:00Z").getTime() / 1000;
// Add 7 days to a timestamp
const nextWeek = now + 7 * 24 * 60 * 60;
// Compare two timestamps
const isExpired = now > expiresAt;
// Get individual components
const year = date.getFullYear();
const month = date.getMonth() + 1; // 0-indexed
const day = date.getDate();
const hours = date.getHours();
import time
from datetime import datetime, timezone, timedelta
# Current Unix timestamp (seconds)
now = int(time.time())
# Current timestamp in milliseconds
ms = int(time.time() * 1000)
# Convert timestamp to datetime
dt = datetime.fromtimestamp(1710492223, tz=timezone.utc)
# Format to ISO 8601
dt.isoformat() # "2024-03-15T12:23:43+00:00"
# Custom format with strftime
dt.strftime("%Y-%m-%d %H:%M:%S") # "2024-03-15 12:23:43"
dt.strftime("%B %d, %Y %I:%M %p") # "March 15, 2024 12:23 PM"
# Parse a date string to timestamp
parsed = datetime.strptime("2024-03-15 08:00:00", "%Y-%m-%d %H:%M:%S")
ts = int(parsed.replace(tzinfo=timezone.utc).timestamp())
# Add 7 days
next_week = dt + timedelta(days=7)
# Convert between timezones
eastern = dt.astimezone(timezone(timedelta(hours=-5)))
# Get individual components
year, month, day = dt.year, dt.month, dt.day
import time
from datetime import datetime, timezone, timedelta
# Current Unix timestamp (seconds)
now = int(time.time())
# Current timestamp in milliseconds
ms = int(time.time() * 1000)
# Convert timestamp to datetime
dt = datetime.fromtimestamp(1710492223, tz=timezone.utc)
# Format to ISO 8601
dt.isoformat() # "2024-03-15T12:23:43+00:00"
# Custom format with strftime
dt.strftime("%Y-%m-%d %H:%M:%S") # "2024-03-15 12:23:43"
dt.strftime("%B %d, %Y %I:%M %p") # "March 15, 2024 12:23 PM"
# Parse a date string to timestamp
parsed = datetime.strptime("2024-03-15 08:00:00", "%Y-%m-%d %H:%M:%S")
ts = int(parsed.replace(tzinfo=timezone.utc).timestamp())
# Add 7 days
next_week = dt + timedelta(days=7)
# Convert between timezones
eastern = dt.astimezone(timezone(timedelta(hours=-5)))
# Get individual components
year, month, day = dt.year, dt.month, dt.day
// Type-safe timestamp utilities
// Current Unix timestamp (seconds)
const now: number = Math.floor(Date.now() / 1000);
// Convert timestamp to Date
function fromUnix(ts: number): Date {
return new Date(ts * 1000);
}
// Format with Intl.DateTimeFormat (no library needed)
function formatDate(ts: number, tz: string = "UTC"): string {
return new Intl.DateTimeFormat("en-US", {
timeZone: tz,
year: "numeric",
month: "short",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false,
}).format(fromUnix(ts));
}
// Relative time (e.g. "3 hours ago")
function timeAgo(ts: number): string {
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
const diff = ts - Math.floor(Date.now() / 1000);
const absDiff = Math.abs(diff);
if (absDiff < 60) return rtf.format(diff, "second");
if (absDiff < 3600) return rtf.format(Math.round(diff / 60), "minute");
if (absDiff < 86400) return rtf.format(Math.round(diff / 3600), "hour");
return rtf.format(Math.round(diff / 86400), "day");
}
// ISO 8601 to Unix timestamp
const ts = Math.floor(new Date("2024-03-15T08:00:00Z").getTime() / 1000);
// Type-safe timestamp utilities
// Current Unix timestamp (seconds)
const now: number = Math.floor(Date.now() / 1000);
// Convert timestamp to Date
function fromUnix(ts: number): Date {
return new Date(ts * 1000);
}
// Format with Intl.DateTimeFormat (no library needed)
function formatDate(ts: number, tz: string = "UTC"): string {
return new Intl.DateTimeFormat("en-US", {
timeZone: tz,
year: "numeric",
month: "short",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false,
}).format(fromUnix(ts));
}
// Relative time (e.g. "3 hours ago")
function timeAgo(ts: number): string {
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
const diff = ts - Math.floor(Date.now() / 1000);
const absDiff = Math.abs(diff);
if (absDiff < 60) return rtf.format(diff, "second");
if (absDiff < 3600) return rtf.format(Math.round(diff / 60), "minute");
if (absDiff < 86400) return rtf.format(Math.round(diff / 3600), "hour");
return rtf.format(Math.round(diff / 86400), "day");
}
// ISO 8601 to Unix timestamp
const ts = Math.floor(new Date("2024-03-15T08:00:00Z").getTime() / 1000);
import java.time.*;
import java.time.format.DateTimeFormatter;
public class TimestampExample {
public static void main(String[] args) {
// Current Unix timestamp (seconds)
long now = Instant.now().getEpochSecond();
// Current timestamp in milliseconds
long ms = System.currentTimeMillis();
// Convert timestamp to Instant
Instant instant = Instant.ofEpochSecond(1710492223L);
// Format to ISO 8601
instant.toString(); // "2024-03-15T12:23:43Z"
// Convert to ZonedDateTime for formatting
ZonedDateTime zdt = instant.atZone(ZoneId.of("America/New_York"));
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
String formatted = zdt.format(fmt); // "2024-03-15 08:23:43 EDT"
// Parse a date string to timestamp
LocalDateTime ldt = LocalDateTime.parse("2024-03-15T08:00:00");
long ts = ldt.toEpochSecond(ZoneOffset.UTC);
// Add 7 days
Instant nextWeek = instant.plus(Duration.ofDays(7));
// Compare timestamps
boolean isAfter = Instant.now().isAfter(instant);
}
}
import java.time.*;
import java.time.format.DateTimeFormatter;
public class TimestampExample {
public static void main(String[] args) {
// Current Unix timestamp (seconds)
long now = Instant.now().getEpochSecond();
// Current timestamp in milliseconds
long ms = System.currentTimeMillis();
// Convert timestamp to Instant
Instant instant = Instant.ofEpochSecond(1710492223L);
// Format to ISO 8601
instant.toString(); // "2024-03-15T12:23:43Z"
// Convert to ZonedDateTime for formatting
ZonedDateTime zdt = instant.atZone(ZoneId.of("America/New_York"));
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
String formatted = zdt.format(fmt); // "2024-03-15 08:23:43 EDT"
// Parse a date string to timestamp
LocalDateTime ldt = LocalDateTime.parse("2024-03-15T08:00:00");
long ts = ldt.toEpochSecond(ZoneOffset.UTC);
// Add 7 days
Instant nextWeek = instant.plus(Duration.ofDays(7));
// Compare timestamps
boolean isAfter = Instant.now().isAfter(instant);
}
}
package main
import (
"fmt"
"time"
)
func main() {
// Current Unix timestamp (seconds)
now := time.Now().Unix()
// Current timestamp in milliseconds
ms := time.Now().UnixMilli()
// Convert timestamp to Time
t := time.Unix(1710492223, 0).UTC()
// Format to ISO 8601
t.Format(time.RFC3339) // "2024-03-15T12:23:43Z"
// Custom format (Go uses reference time: Mon Jan 2 15:04:05 2006)
t.Format("2006-01-02 15:04:05") // "2024-03-15 12:23:43"
t.Format("Jan 02, 2006 3:04 PM") // "Mar 15, 2024 12:23 PM"
// Parse a date string to timestamp
parsed, _ := time.Parse("2006-01-02 15:04:05", "2024-03-15 08:00:00")
ts := parsed.Unix()
// Add 7 days
nextWeek := t.AddDate(0, 0, 7)
// Convert to a specific timezone
loc, _ := time.LoadLocation("America/New_York")
eastern := t.In(loc)
// Get individual components
year, month, day := t.Date()
hour, min, sec := t.Clock()
fmt.Println(now, ms, ts, nextWeek, eastern, year, month, day, hour, min, sec)
}
package main
import (
"fmt"
"time"
)
func main() {
// Current Unix timestamp (seconds)
now := time.Now().Unix()
// Current timestamp in milliseconds
ms := time.Now().UnixMilli()
// Convert timestamp to Time
t := time.Unix(1710492223, 0).UTC()
// Format to ISO 8601
t.Format(time.RFC3339) // "2024-03-15T12:23:43Z"
// Custom format (Go uses reference time: Mon Jan 2 15:04:05 2006)
t.Format("2006-01-02 15:04:05") // "2024-03-15 12:23:43"
t.Format("Jan 02, 2006 3:04 PM") // "Mar 15, 2024 12:23 PM"
// Parse a date string to timestamp
parsed, _ := time.Parse("2006-01-02 15:04:05", "2024-03-15 08:00:00")
ts := parsed.Unix()
// Add 7 days
nextWeek := t.AddDate(0, 0, 7)
// Convert to a specific timezone
loc, _ := time.LoadLocation("America/New_York")
eastern := t.In(loc)
// Get individual components
year, month, day := t.Date()
hour, min, sec := t.Clock()
fmt.Println(now, ms, ts, nextWeek, eastern, year, month, day, hour, min, sec)
}
use chrono::{DateTime, NaiveDateTime, TimeZone, Utc, Duration, FixedOffset};
fn main() {
// Current Unix timestamp (seconds)
let now = Utc::now().timestamp();
// Current timestamp in milliseconds
let ms = Utc::now().timestamp_millis();
// Convert timestamp to DateTime
let dt = DateTime::from_timestamp(1710492223, 0).unwrap();
// Format to ISO 8601
dt.to_rfc3339(); // "2024-03-15T12:23:43+00:00"
// Custom format with strftime
dt.format("%Y-%m-%d %H:%M:%S").to_string(); // "2024-03-15 12:23:43"
dt.format("%B %d, %Y %I:%M %p").to_string(); // "March 15, 2024 12:23 PM"
// Parse a date string to timestamp
let parsed = NaiveDateTime::parse_from_str(
"2024-03-15 08:00:00", "%Y-%m-%d %H:%M:%S"
).unwrap();
let ts = parsed.and_utc().timestamp();
// Add 7 days
let next_week = dt + Duration::days(7);
// Convert to a fixed offset timezone (UTC-5)
let eastern = FixedOffset::west_opt(5 * 3600).unwrap();
let est_time = dt.with_timezone(&eastern);
println!("{now} {ms} {ts} {next_week} {est_time}");
}
use chrono::{DateTime, NaiveDateTime, TimeZone, Utc, Duration, FixedOffset};
fn main() {
// Current Unix timestamp (seconds)
let now = Utc::now().timestamp();
// Current timestamp in milliseconds
let ms = Utc::now().timestamp_millis();
// Convert timestamp to DateTime
let dt = DateTime::from_timestamp(1710492223, 0).unwrap();
// Format to ISO 8601
dt.to_rfc3339(); // "2024-03-15T12:23:43+00:00"
// Custom format with strftime
dt.format("%Y-%m-%d %H:%M:%S").to_string(); // "2024-03-15 12:23:43"
dt.format("%B %d, %Y %I:%M %p").to_string(); // "March 15, 2024 12:23 PM"
// Parse a date string to timestamp
let parsed = NaiveDateTime::parse_from_str(
"2024-03-15 08:00:00", "%Y-%m-%d %H:%M:%S"
).unwrap();
let ts = parsed.and_utc().timestamp();
// Add 7 days
let next_week = dt + Duration::days(7);
// Convert to a fixed offset timezone (UTC-5)
let eastern = FixedOffset::west_opt(5 * 3600).unwrap();
let est_time = dt.with_timezone(&eastern);
println!("{now} {ms} {ts} {next_week} {est_time}");
}
<?php
// Current Unix timestamp (seconds)
$now = time();
// Current timestamp in milliseconds
$ms = (int)(microtime(true) * 1000);
// Convert timestamp to formatted date
$date = date('Y-m-d H:i:s', 1710492223); // "2024-03-15 12:23:43"
// Format to ISO 8601
$iso = date('c', 1710492223); // "2024-03-15T12:23:43+00:00"
// Custom format
date('F j, Y g:i A', 1710492223); // "March 15, 2024 12:23 PM"
// Parse a date string to timestamp
$ts = strtotime('2024-03-15 08:00:00');
// Using DateTime object (recommended)
$dt = new DateTime('@1710492223');
$dt->setTimezone(new DateTimeZone('America/New_York'));
echo $dt->format('Y-m-d H:i:s T'); // "2024-03-15 08:23:43 EDT"
// Add 7 days
$dt->modify('+7 days');
// Create from format
$parsed = DateTime::createFromFormat('Y-m-d H:i:s', '2024-03-15 08:00:00');
$ts = $parsed->getTimestamp();
// Compare dates
$isExpired = time() > $expiresAt;
<?php
// Current Unix timestamp (seconds)
$now = time();
// Current timestamp in milliseconds
$ms = (int)(microtime(true) * 1000);
// Convert timestamp to formatted date
$date = date('Y-m-d H:i:s', 1710492223); // "2024-03-15 12:23:43"
// Format to ISO 8601
$iso = date('c', 1710492223); // "2024-03-15T12:23:43+00:00"
// Custom format
date('F j, Y g:i A', 1710492223); // "March 15, 2024 12:23 PM"
// Parse a date string to timestamp
$ts = strtotime('2024-03-15 08:00:00');
// Using DateTime object (recommended)
$dt = new DateTime('@1710492223');
$dt->setTimezone(new DateTimeZone('America/New_York'));
echo $dt->format('Y-m-d H:i:s T'); // "2024-03-15 08:23:43 EDT"
// Add 7 days
$dt->modify('+7 days');
// Create from format
$parsed = DateTime::createFromFormat('Y-m-d H:i:s', '2024-03-15 08:00:00');
$ts = $parsed->getTimestamp();
// Compare dates
$isExpired = time() > $expiresAt;
require 'time'
# Current Unix timestamp (seconds)
now = Time.now.to_i
# Current timestamp in milliseconds
ms = (Time.now.to_f * 1000).to_i
# Convert timestamp to Time object
t = Time.at(1710492223).utc
# Format to ISO 8601
t.iso8601 # "2024-03-15T12:23:43Z"
# Custom format with strftime
t.strftime('%Y-%m-%d %H:%M:%S') # "2024-03-15 12:23:43"
t.strftime('%B %d, %Y %I:%M %p') # "March 15, 2024 12:23 PM"
# Parse a date string to timestamp
ts = Time.parse('2024-03-15 08:00:00 UTC').to_i
# Add 7 days (seconds)
next_week = t + (7 * 24 * 60 * 60)
# Convert to a specific timezone
ENV['TZ'] = 'America/New_York'
eastern = Time.at(1710492223)
ENV['TZ'] = nil
# Get individual components
year = t.year
month = t.month
day = t.day
hour = t.hour
require 'time'
# Current Unix timestamp (seconds)
now = Time.now.to_i
# Current timestamp in milliseconds
ms = (Time.now.to_f * 1000).to_i
# Convert timestamp to Time object
t = Time.at(1710492223).utc
# Format to ISO 8601
t.iso8601 # "2024-03-15T12:23:43Z"
# Custom format with strftime
t.strftime('%Y-%m-%d %H:%M:%S') # "2024-03-15 12:23:43"
t.strftime('%B %d, %Y %I:%M %p') # "March 15, 2024 12:23 PM"
# Parse a date string to timestamp
ts = Time.parse('2024-03-15 08:00:00 UTC').to_i
# Add 7 days (seconds)
next_week = t + (7 * 24 * 60 * 60)
# Convert to a specific timezone
ENV['TZ'] = 'America/New_York'
eastern = Time.at(1710492223)
ENV['TZ'] = nil
# Get individual components
year = t.year
month = t.month
day = t.day
hour = t.hour
using System;
using System.Globalization;
// Current Unix timestamp (seconds)
long now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
// Current timestamp in milliseconds
long ms = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
// Convert timestamp to DateTimeOffset
var dto = DateTimeOffset.FromUnixTimeSeconds(1710492223);
// Format to ISO 8601
dto.ToString("o"); // "2024-03-15T12:23:43.0000000+00:00"
// Custom format
dto.ToString("yyyy-MM-dd HH:mm:ss"); // "2024-03-15 12:23:43"
dto.ToString("MMMM dd, yyyy h:mm tt"); // "March 15, 2024 12:23 PM"
// Parse a date string to timestamp
var parsed = DateTimeOffset.Parse("2024-03-15T08:00:00Z");
long ts = parsed.ToUnixTimeSeconds();
// Add 7 days
var nextWeek = dto.AddDays(7);
// Convert to a specific timezone
var eastern = TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
var estTime = TimeZoneInfo.ConvertTime(dto, eastern);
// Compare timestamps
bool isExpired = DateTimeOffset.UtcNow > dto;
using System;
using System.Globalization;
// Current Unix timestamp (seconds)
long now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
// Current timestamp in milliseconds
long ms = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
// Convert timestamp to DateTimeOffset
var dto = DateTimeOffset.FromUnixTimeSeconds(1710492223);
// Format to ISO 8601
dto.ToString("o"); // "2024-03-15T12:23:43.0000000+00:00"
// Custom format
dto.ToString("yyyy-MM-dd HH:mm:ss"); // "2024-03-15 12:23:43"
dto.ToString("MMMM dd, yyyy h:mm tt"); // "March 15, 2024 12:23 PM"
// Parse a date string to timestamp
var parsed = DateTimeOffset.Parse("2024-03-15T08:00:00Z");
long ts = parsed.ToUnixTimeSeconds();
// Add 7 days
var nextWeek = dto.AddDays(7);
// Convert to a specific timezone
var eastern = TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
var estTime = TimeZoneInfo.ConvertTime(dto, eastern);
// Compare timestamps
bool isExpired = DateTimeOffset.UtcNow > dto;
import Foundation
// Current Unix timestamp (seconds)
let now = Int(Date().timeIntervalSince1970)
// Current timestamp in milliseconds
let ms = Int(Date().timeIntervalSince1970 * 1000)
// Convert timestamp to Date
let date = Date(timeIntervalSince1970: 1710492223)
// Format to ISO 8601
let isoFormatter = ISO8601DateFormatter()
isoFormatter.string(from: date) // "2024-03-15T12:23:43Z"
// Custom format with DateFormatter
let fmt = DateFormatter()
fmt.dateFormat = "yyyy-MM-dd HH:mm:ss"
fmt.timeZone = TimeZone(identifier: "UTC")
fmt.string(from: date) // "2024-03-15 12:23:43"
// Locale-aware formatting
fmt.dateStyle = .full
fmt.timeStyle = .long
fmt.timeZone = TimeZone(identifier: "America/New_York")
fmt.string(from: date) // "Friday, March 15, 2024 at 8:23:43 AM EDT"
// Parse a date string to timestamp
fmt.dateFormat = "yyyy-MM-dd HH:mm:ss"
if let parsed = fmt.date(from: "2024-03-15 08:00:00") {
let ts = Int(parsed.timeIntervalSince1970)
}
// Add 7 days
let nextWeek = Calendar.current.date(byAdding: .day, value: 7, to: date)!
import Foundation
// Current Unix timestamp (seconds)
let now = Int(Date().timeIntervalSince1970)
// Current timestamp in milliseconds
let ms = Int(Date().timeIntervalSince1970 * 1000)
// Convert timestamp to Date
let date = Date(timeIntervalSince1970: 1710492223)
// Format to ISO 8601
let isoFormatter = ISO8601DateFormatter()
isoFormatter.string(from: date) // "2024-03-15T12:23:43Z"
// Custom format with DateFormatter
let fmt = DateFormatter()
fmt.dateFormat = "yyyy-MM-dd HH:mm:ss"
fmt.timeZone = TimeZone(identifier: "UTC")
fmt.string(from: date) // "2024-03-15 12:23:43"
// Locale-aware formatting
fmt.dateStyle = .full
fmt.timeStyle = .long
fmt.timeZone = TimeZone(identifier: "America/New_York")
fmt.string(from: date) // "Friday, March 15, 2024 at 8:23:43 AM EDT"
// Parse a date string to timestamp
fmt.dateFormat = "yyyy-MM-dd HH:mm:ss"
if let parsed = fmt.date(from: "2024-03-15 08:00:00") {
let ts = Int(parsed.timeIntervalSince1970)
}
// Add 7 days
let nextWeek = Calendar.current.date(byAdding: .day, value: 7, to: date)!
import java.time.*
import java.time.format.DateTimeFormatter
fun main() {
// Current Unix timestamp (seconds)
val now = Instant.now().epochSecond
// Current timestamp in milliseconds
val ms = System.currentTimeMillis()
// Convert timestamp to Instant
val instant = Instant.ofEpochSecond(1710492223L)
// Format to ISO 8601
instant.toString() // "2024-03-15T12:23:43Z"
// Convert to ZonedDateTime for custom formatting
val zdt = instant.atZone(ZoneId.of("America/New_York"))
val fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z")
zdt.format(fmt) // "2024-03-15 08:23:43 EDT"
// Parse a date string to timestamp
val parsed = LocalDateTime.parse("2024-03-15T08:00:00")
val ts = parsed.toEpochSecond(ZoneOffset.UTC)
// Add 7 days
val nextWeek = instant.plus(Duration.ofDays(7))
// Relative time calculation
val duration = Duration.between(instant, Instant.now())
println("${duration.toDays()} days ago")
}
import java.time.*
import java.time.format.DateTimeFormatter
fun main() {
// Current Unix timestamp (seconds)
val now = Instant.now().epochSecond
// Current timestamp in milliseconds
val ms = System.currentTimeMillis()
// Convert timestamp to Instant
val instant = Instant.ofEpochSecond(1710492223L)
// Format to ISO 8601
instant.toString() // "2024-03-15T12:23:43Z"
// Convert to ZonedDateTime for custom formatting
val zdt = instant.atZone(ZoneId.of("America/New_York"))
val fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z")
zdt.format(fmt) // "2024-03-15 08:23:43 EDT"
// Parse a date string to timestamp
val parsed = LocalDateTime.parse("2024-03-15T08:00:00")
val ts = parsed.toEpochSecond(ZoneOffset.UTC)
// Add 7 days
val nextWeek = instant.plus(Duration.ofDays(7))
// Relative time calculation
val duration = Duration.between(instant, Instant.now())
println("${duration.toDays()} days ago")
}