UNIX ٹائم اسٹیمپ کیا ہے؟
A UNIX timestamp (also called epoch time, POSIX time, or simply Unix time) is a way to represent a point in time as a single integer — the number of seconds (or milliseconds) elapsed since the UNIX Epoch: January 1, 1970, 00:00:00 UTC.
It is the universal language for time across operating systems, databases, and programming languages because it is timezone-agnostic, compact, and sortable. Any negative value represents a moment before 1970.
Why milliseconds instead of seconds?
The original UNIX standard measured time in seconds. Modern languages (JavaScript, Java) use milliseconds for sub-second precision without floating-point arithmetic. Some systems (Linux clock_gettime, Go's time.Now().UnixNano()) go further with nanoseconds. Always check your language's default unit when working with timestamps.
ٹائم اسٹامپس کو عالمگیر کیا بناتا ہے؟
- کوئی ٹائم زون ابہام نہیں — ہمیشہ UTC کے نسبت
- واحد انٹیجر — اسٹور، موازنہ اور ترسیل آسان
- قدرتی طور پر قابلِ ترتیب — بڑی قدر = بعد کا وقت
- Unambiguous during DST changes — wall-clock times can be ambiguous, timestamps never are
Java میں System.currentTimeMillis()
System.currentTimeMillis() Java کا بلٹ ان طریقہ ہے جو موجودہ وقت کو یکم جنوری 1970 UTC سے ملی سیکنڈ کی تعداد میں واپس کرتا ہے۔ یہ JavaScript کے Date.now() کا Java مساوی ہے۔
public class TimestampExample {
public static void main(String[] args) {
// Get current time in milliseconds
long millis = System.currentTimeMillis();
System.out.println("Millis: " + millis);
// Convert to Date
java.util.Date date = new java.util.Date(millis);
System.out.println("Date: " + date);
// Modern API (Java 8+) — recommended
java.time.Instant instant = java.time.Instant.ofEpochMilli(millis);
System.out.println("Instant: " + instant);
}
}
Modern Java time API (Java 8+ recommended)
Java 8 introduced the java.time package which is far superior to Date/Calendar.
import java.time.*;
// Current timestamp in milliseconds
long ms = Instant.now().toEpochMilli();
// Current timestamp in seconds
long sec = Instant.now().getEpochSecond();
// Convert millis back to Instant
Instant instant = Instant.ofEpochMilli(ms);
// Format as ISO 8601
String iso = instant.toString(); // "2026-03-16T10:30:00Z"
// Format with timezone
ZonedDateTime zdt = instant.atZone(ZoneId.of("America/New_York"));
String formatted = zdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME);
System.nanoTime() بمقابلہ currentTimeMillis()
System.nanoTime() returns nanoseconds elapsed since some arbitrary origin — it is not tied to the UNIX epoch. Use it only for measuring elapsed time intervals, not for wall-clock timestamps.
// Measure elapsed time (correct)
long start = System.nanoTime();
doSomething();
long elapsed = System.nanoTime() - start;
System.out.printf("Took %.2f ms%n", elapsed / 1_000_000.0);
// Wall clock (correct)
long now = System.currentTimeMillis();
java.time سے Instant.now().toEpochMilli() کو ترجیح دیں۔ یہ ناقابلِ تغیر، تھریڈ سیف، اور System.currentTimeMillis() سے زیادہ واضح ہے۔UTC کو سمجھنا
UTC (Coordinated Universal Time) is the primary time standard by which the world regulates clocks and time. It is the successor to Greenwich Mean Time (GMT) and the basis for all UNIX timestamps.
UTC بمقابلہ GMT — فرق کیا ہے؟
GMT (Greenwich Mean Time) is a timezone originally based on the mean solar time at the Royal Observatory in Greenwich, London. UTC is a modern atomic-clock-based standard. They read the same offset (UTC+0 = GMT+0), but UTC is more precise — it includes leap seconds to account for irregularities in Earth's rotation.
- For programming purposes, UTC and GMT are interchangeable. Most databases, APIs, and systems store time in UTC.
- Leap seconds are added to UTC periodically (since 1972) — most computer systems "smear" or skip them and do not expose them to application code.
سب کچھ UTC میں کیوں اسٹور کریں؟
- Timezone rules change (governments alter DST schedules). UTC never changes.
- Comparing timestamps across timezones is trivial — just compare the UTC values directly.
- ڈے لائٹ سیونگ ٹائم (DST) کی تبدیلیاں مبہم وال-کلاک اوقات پیدا کرتی ہیں۔ UTC میں کوئی ابہام نہیں۔
- ڈیٹا بیس (PostgreSQL، MySQL) جن میں
TIMESTAMP WITH TIME ZONEہو، اندرونی طور پر UTC میں قدریں اسٹور کرتے ہیں۔
Getting UTC time in various languages
const now = new Date();
console.log(now.toUTCString()); // "Mon, 16 Mar 2026 10:30:00 GMT"
console.log(now.toISOString()); // "2026-03-16T10:30:00.000Z"
console.log(Date.now()); // ms since epoch (always UTC)
from datetime import datetime, timezone
# UTC-aware datetime
now_utc = datetime.now(timezone.utc)
print(now_utc.isoformat()) # "2026-03-16T10:30:00+00:00"
# Unix timestamp (seconds)
import time
ts = time.time() # float seconds
ٹائم زونز کے ساتھ کام کرنا
ٹائم زون سافٹ ویئر میں بگ کے سب سے عام ذرائع میں سے ایک ہیں۔ یہ سمجھنا کہ وہ کیسے کام کرتے ہیں — خاص طور پر ڈے لائٹ سیونگ ٹائم (DST) — ڈیبگنگ کے گھنٹے بچاتا ہے۔
ٹائم زون آفسیٹ — یہ کیسے کام کرتے ہیں
ٹائم زون ایک ایسا علاقہ ہے جو یکساں معیاری وقت کا مشاہدہ کرتا ہے۔ آفسیٹ کوآرڈینیٹڈ یونیورسل ٹائم کے نسبت UTC+HH:MM یا UTC-HH:MM کے طور پر ظاہر کیے جاتے ہیں۔
UTC+05:30= انڈیا اسٹینڈرڈ ٹائم (IST) — UTC سے 5 گھنٹے 30 منٹ آگےUTC-05:00= ایسٹرن اسٹینڈرڈ ٹائم (EST)UTC+09:00= جاپان اسٹینڈرڈ ٹائم (JST) — کوئی DST نہیں
+05:30 کو کبھی ہارڈکوڈ نہ کریں۔ IANA ٹائم زون ناموں کا استعمال کریں (مثلاً "Asia/Kolkata") کیونکہ سیاسی فیصلوں کی وجہ سے آفسیٹ بدل سکتے ہیں۔ڈے لائٹ سیونگ ٹائم (DST) کی وضاحت
DST گرمیوں کے مہینوں میں شام کی روشنی بڑھانے کے لیے گھڑیوں کو 1 گھنٹہ آگے کرنے کا عمل ہے۔ یہ بہت سے (لیکن سب نہیں) ممالک میں رائج ہے۔
DST کی عام غلطیاں:
- Clocks "spring forward" — 2:00 AM becomes 3:00 AM. The hour between 2-3 AM does not exist.
- Clocks "fall back" — 2:00 AM becomes 1:00 AM again. The hour between 1-2 AM occurs twice.
- Events scheduled at ambiguous times can be mis-scheduled by exactly 1 hour.
// Safe: use Intl.DateTimeFormat with IANA timezone name
const now = new Date();
const fmt = new Intl.DateTimeFormat('en-US', {
timeZone: 'America/New_York',
dateStyle: 'full',
timeStyle: 'long'
});
console.log(fmt.format(now));
// "Monday, March 16, 2026 at 6:30:00 AM EDT"
وہ ممالک جو DST نہیں مانتے
Many countries near the equator and several others do not observe DST, making their offsets constant year-round:
- جاپان (UTC+9)، چین (UTC+8)، بھارت (UTC+5:30)
- زیادہ تر افریقہ، جنوب مشرقی ایشیا، اور مشرقِ وسطیٰ
- Arizona (US), Hawaii (US) — state-level exceptions within DST countries
Common timezone pitfalls in code
- ٹائم زون کے بغیر تاریخیں پارس کرنا — JavaScript میں
new Date("2026-03-16")کو UTC آدھی رات کے طور پر پارس کیا جاتا ہے، لیکنnew Date("03/16/2026")کو مقامی آدھی رات کے طور پر۔ ہمیشہ ٹائم زون واضح طور پر بتائیں۔ - ڈیٹا بیس میں مقامی وقت اسٹور کرنا — ہمیشہ UTC اسٹور کریں، آؤٹ پٹ پر تبدیل کریں۔
- DST حدود کے پار ٹائم اسٹامپ کا موازنہ — پہلے UTC میں تبدیل کریں۔
- بار بار ہونے والے واقعات کا شیڈول — آفسیٹ نہیں، IANA ناموں کا استعمال کریں تاکہ مستقبل کی DST تبدیلیاں خودبخود ہوں۔
// DANGER: Different parsing behavior!
new Date("2026-03-16").toISOString();
// "2026-03-16T00:00:00.000Z" — UTC midnight ✓
new Date("03/16/2026").toISOString();
// "2026-03-16T05:00:00.000Z" — local midnight in UTC-5 !
// SAFE: always append time and Z for UTC
new Date("2026-03-16T00:00:00Z").toISOString();
// "2026-03-16T00:00:00.000Z" ✓
ٹائم اسٹیمپ فارمیٹس کی وضاحت
Multiple standards define how timestamps are written as human-readable strings. Choosing the right format depends on your use case.
| فارمیٹ | مثال | Best Used For |
|---|---|---|
| ISO 8601 تجویز کردہ | 2026-03-16T10:30:00.000Z |
APIs، ڈیٹا بیس، لاگ فائلیں، JSON پے لوڈ |
| RFC 3339 | 2026-03-16T10:30:00+00:00 |
انٹرنیٹ پروٹوکول، ATOM فیڈز — ISO 8601 کا سخت پروفائل |
| RFC 2616 (HTTP) | Mon, 16 Mar 2026 10:30:00 GMT |
HTTP ہیڈر (Last-Modified، Expires، Cookie) |
| Unix (سیکنڈ) | 1773835800 |
اسٹوریج، موازنہ، ریاضی — سب سے چھوٹی نمائندگی |
| Unix (ملی سیکنڈ) | 1773835800000 |
JavaScript، Java، سب سیکنڈ درستگی |
| جولین تاریخ | 2461110.9375 |
Astronomy, scientific computing — days since Jan 1, 4713 BC |
ISO 8601 گہرائی میں
ISO 8601 is the international standard for date/time representation. Key components:
2026 - 03 - 16 T 10 : 30 : 00 . 000 Z
Year Month Day Hr Min Sec Ms UTC
With offset:
2026-03-16T10:30:00.000+07:00 (UTC+7)
2026-03-16T10:30:00.000-05:00 (UTC-5)
Date only:
2026-03-16
Week notation:
2026-W12-1 (Year-Week-Weekday)
جولین تاریخ کا حساب
The Julian Date (JD) is a continuous count of days since the beginning of the Julian Period (January 1, 4713 BC). The formula from a Unix timestamp (milliseconds) is:
// Julian Date from Unix milliseconds
julianDate = (unixMs / 86400000) + 2440587.5;
// Example: Jan 1, 1970 00:00:00 UTC
// (0 / 86400000) + 2440587.5 = 2440587.5
// JavaScript
const jd = (Date.now() / 86400000) + 2440587.5;
عام ٹائم اسٹیمپ آپریشنز
Because timestamps are just numbers, arithmetic is straightforward. Here are the most common operations:
Add / Subtract time
const now = Date.now(); // ms
// Constants (in milliseconds)
const SECOND = 1000;
const MINUTE = 60 * SECOND;
const HOUR = 60 * MINUTE;
const DAY = 24 * HOUR;
const WEEK = 7 * DAY;
// Add 30 days
const in30Days = now + (30 * DAY);
// Subtract 2 hours
const twoHoursAgo = now - (2 * HOUR);
// Add 1 year (approximate — use date lib for exact)
const inOneYear = now + (365.25 * DAY);
30 * DAY is always exactly 30 days in UTC. However, if you need "1 calendar month from now," use a date library (date-fns, Luxon, Day.js) because months have varying lengths.Compare timestamps
const ts1 = 1700000000000;
const ts2 = 1773835800000;
// Is ts1 before ts2?
ts1 < ts2; // true
// Difference in days
const diffDays = (ts2 - ts1) / (1000 * 60 * 60 * 24);
// Is a timestamp in the past?
const isExpired = ts1 < Date.now();
// Sort array of timestamps (ascending)
const sorted = [ts2, ts1, 1000000000000].sort((a, b) => a - b);
تاریخ کا حساب — دن، ہفتے، مہینے کا آغاز/اختتام
// Start of today (UTC midnight)
const now = new Date();
const startOfDay = new Date(
Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate())
).getTime();
// Start of this month
const startOfMonth = new Date(
Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1)
).getTime();
// End of today (last ms)
const endOfDay = startOfDay + (24 * 60 * 60 * 1000) - 1;
Format relative time ("2 hours ago")
function relativeTime(ms) {
const diff = ms - Date.now();
const abs = Math.abs(diff);
const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
if (abs < 60000) return rtf.format(Math.round(diff/1000), 'second');
if (abs < 3600000) return rtf.format(Math.round(diff/60000), 'minute');
if (abs < 86400000) return rtf.format(Math.round(diff/3600000), 'hour');
return rtf.format(Math.round(diff/86400000), 'day');
}
relativeTime(Date.now() - 7200000); // "2 hours ago"
relativeTime(Date.now() + 86400000); // "tomorrow"
زبان کا فوری حوالہ
30 سے زائد پروگرامنگ زبانوں میں UNIX ایپاک سے ملی سیکنڈ کے طور پر موجودہ وقت حاصل کرنے کا طریقہ۔
| زبان | Code | اکائی |
|---|---|---|
| JavaScript | Date.now() | ms |
| TypeScript | Date.now() | ms |
| Java | System.currentTimeMillis() | ms |
| Kotlin | System.currentTimeMillis() | ms |
| Python | int(time.time() * 1000) | ms (from s) |
| C# | DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() | ms |
| Go | time.Now().UnixMilli() | ms |
| Rust | SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() | ms |
| PHP | round(microtime(true) * 1000) | ms (from s) |
| Ruby | Time.now.to_i * 1000 | ms (from s) |
| Swift | Int(Date().timeIntervalSince1970 * 1000) | ms |
| Objective-C | [[NSDate date] timeIntervalSince1970] * 1000 | ms |
| C (POSIX) | clock_gettime(CLOCK_REALTIME, &ts); ts.tv_sec*1000 + ts.tv_nsec/1e6 | ms |
| C++ | chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count() | ms |
| Scala | System.currentTimeMillis() | ms |
| Groovy | System.currentTimeMillis() | ms |
| Perl | int(time() * 1000) | ms (approx) |
| Lua | os.time() * 1000 | ms (approx) |
| R | as.numeric(Sys.time()) * 1000 | ms |
| MATLAB | posixtime(datetime('now','TimeZone','UTC')) * 1000 | ms |
| Bash / Shell | date +%s%3N | ms |
| PowerShell | [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() | ms |
| SQL (PostgreSQL) | EXTRACT(EPOCH FROM NOW()) * 1000 | ms |
| SQL (MySQL) | UNIX_TIMESTAMP(NOW(3)) * 1000 | ms |
| SQL (SQLite) | strftime('%s','now') * 1000 | ms |
| Dart / Flutter | DateTime.now().millisecondsSinceEpoch | ms |
| Haskell | round . (* 1000) <$> getPOSIXTime | ms |
| Elixir | System.system_time(:millisecond) | ms |
| Erlang | erlang:system_time(millisecond) | ms |
| Clojure | (System/currentTimeMillis) | ms |
| F# | DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() | ms |
| Julia | round(Int, time() * 1000) | ms |
| Nim | getTime().toUnix() * 1000 | ms |
| Crystal | Time.utc.to_unix_ms | ms |
| Zig | std.time.milliTimestamp() | ms |
اعلیٰ زبانوں کے لیے تفصیلی مثالیں
import time
from datetime import datetime, timezone
# Milliseconds since epoch
ms = int(time.time() * 1000)
print(ms) # e.g. 1773835800123
# Convert ms back to UTC datetime
dt = datetime.fromtimestamp(ms / 1000, tz=timezone.utc)
print(dt.isoformat()) # "2026-03-16T10:30:00.123000+00:00"
package main
import (
"fmt"
"time"
)
func main() {
// Milliseconds (Go 1.17+)
ms := time.Now().UnixMilli()
fmt.Println(ms)
// Convert back to time.Time
t := time.UnixMilli(ms)
fmt.Println(t.UTC().Format(time.RFC3339Nano))
}
using System;
// Milliseconds since epoch
long ms = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
Console.WriteLine(ms);
// Convert back to DateTimeOffset
var dt = DateTimeOffset.FromUnixTimeMilliseconds(ms);
Console.WriteLine(dt.ToString("o")); // ISO 8601
use std::time::{SystemTime, UNIX_EPOCH};
fn main() {
let ms = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time went backwards")
.as_millis();
println!("{}", ms);
}