1. Converter Tools > 
  2. Cheatsheets > 
  3. Time > 
  4. Unix Timestamp Cheat

Unix Timestamp Cheat

Quick reference for Unix timestamp and date formats.

Unix Timestamp Cheat

A Unix timestamp is the number of seconds since January 1, 1970 00:00:00 UTC (the “epoch”). Some APIs use milliseconds. Always check which one.

Landmarks: 0 is Jan 1, 1970. 86400 is one day later. 1704067200 is Jan 1, 2024 00:00:00 UTC.

Get “now” in code

JavaScript gives milliseconds. For seconds, divide by 1000 and floor.

Date.now()                    // 1734567890123 (ms)
Math.floor(Date.now() / 1000) // 1734567890 (seconds, e.g. for API auth)

Python and PHP use seconds by default.

import time
time.time()  # 1734567890.123
time();  // 1734567890

Turn a timestamp into a date

JavaScript’s Date constructor expects milliseconds. If you have seconds (e.g. from an API), multiply by 1000.

const sec = 1704067200;
new Date(sec * 1000).toISOString();  // "2024-01-01T00:00:00.000Z"
new Date(sec * 1000).toLocaleString();  // locale date/time string

Python:

from datetime import datetime, timezone
datetime.fromtimestamp(1704067200, tz=timezone.utc)
# datetime(2024, 1, 1, 0, 0, tzinfo=timezone.utc)

Turn a date string into a timestamp

ISO 8601 format: YYYY-MM-DDTHH:mm:ss.sssZ. JavaScript:

new Date('2024-01-01T00:00:00.000Z').getTime()  // ms
Math.floor(new Date('2024-01-01T00:00:00.000Z').getTime() / 1000)  // seconds

Python:

from datetime import datetime, timezone
dt = datetime.fromisoformat('2024-01-01T00:00:00+00:00')
int(dt.timestamp())  # 1704067200

Today at midnight UTC

Useful for “start of day” buckets or cache keys that change daily.

from datetime import datetime, timezone
start = datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0)
int(start.timestamp())

JavaScript:

const d = new Date();
d.setUTCHours(0, 0, 0, 0);
Math.floor(d.getTime() / 1000);

Difference in days

Subtract two timestamps (same unit). Divide by 86400 for days.

const old = 1704067200;  // Jan 1, 2024
const now = Math.floor(Date.now() / 1000);
const daysAgo = Math.floor((now - old) / 86400);

ISO 8601 quick reference

Format: YYYY-MM-DDTHH:mm:ss.sssZ. The Z means UTC. Example: 2024-01-01T00:00:00.000Z. Use this when talking to APIs or storing dates in a portable way.

Timezone gotchas

Unix timestamps are always in UTC. When you create a date from a timestamp, the result can be in local time or UTC depending on the API. In JavaScript, new Date(sec * 1000) creates a date in the local timezone for display. Use toISOString() for UTC. In Python, datetime.fromtimestamp(ts) gives local time by default. Use datetime.fromtimestamp(ts, tz=timezone.utc) for UTC. When comparing or storing, stick to one convention (usually UTC) and convert to local only for display.

Local midnight vs UTC midnight. “Start of today” in the user’s timezone is different from “start of today UTC”. For cache keys or daily buckets that should be global, use UTC midnight. For “today’s events” in a calendar, use local midnight (e.g. in JavaScript construct the date at midnight in local time, then get the timestamp).

Start of week (Monday)

Useful for weekly reports or “this week” filters. Get today, go back to the most recent Monday, set time to midnight, then take the timestamp. In Python with datetime: subtract the weekday (0=Monday, 6=Sunday) in days and zero out the time. In JavaScript: similar logic with getDay() (0=Sunday) so you need to map to Monday-based.

from datetime import datetime, timezone, timedelta
now = datetime.now(timezone.utc)
# Monday = 0, so weekday() gives days since Monday
start_of_week = (now - timedelta(days=now.weekday())).replace(hour=0, minute=0, second=0, microsecond=0)
int(start_of_week.timestamp())

Formatting for display

Use the language’s locale APIs so users see dates in their format. JavaScript: toLocaleDateString(), toLocaleString(), or options like { dateStyle: 'short' }. Python: strftime or libraries like babel. For “2 days ago” style relative time, many languages have a library (e.g. date-fns/formatDistanceToNow in JS, or implement with the day difference you already have).

We will add a Unix timestamp converter tool in a future update.

Ad
Converter Tools

  • Home


    • Conversions
      • Length Converter
      • Area Converter
      • Volume Converter
      • Color Converter
      • Weight Converter
      • Temperature Converter
      • Angle Converter
      • Time Converter
      • Data Size Converter
      • Energy Converter
      • Number Base Converter
    • Calculators
      • Lengths
        • Feet and Inches Calculator
        • Pythagorean Theorem Calculator
        • Circle Circumference Calculator
        • Circle Area Calculator
        • Triangle Area Calculator
        • Rectangle Area and Perimeter Calculator
      • Percentages
        • What is X% of Y Calculator
        • X is What Percent of Y Calculator
        • Percentage Change Calculator
        • Add or Subtract Percentage Calculator
      • Time
        • Age Calculator
        • Date Difference Calculator
        • Day of Week Calculator
        • Countdown Calculator
        • Week Number Calculator
      • Body indices
        • BMR Calculator
        • TDEE Calculator
        • Ideal Weight Calculator
        • Body Fat Percentage Calculator
        • Waist-to-Hip Ratio Calculator
        • Calorie Deficit Calculator
        • BMI Calculator
      • Finance
        • Tip Calculator
        • Discount Calculator
        • Simple Interest Calculator
        • Compound Interest Calculator
        • Loan Payment (EMI) Calculator
      • Math
        • Quadratic Equation Solver
        • Factorial Calculator
        • GCD and LCM Calculator
        • Prime Number Checker
    • Dev Tools
      • Base64 Encode and Decode
      • Password Generator
      • QR Code Generator
      • Regex Tester
      • UUID Generator
    • Images
      • Image Conversions
      • Image Compression
      • Image Resize
      • Image Grayscale
      • EXIF Reader
      • Image Filters
      • Edge Detection
      • Image Crop
      • Image Rotate & Flip
      • Image Format Info
      • Image Thumbnail
    • File conversions
      • CSV and Excel Converter
    • PDF editing
      • Merge PDFs
      • Split PDF
      • Extract PDF pages
      • Rotate PDF pages
      • Add watermark to PDF
      • PDF metadata
    • Cheatsheets
      • CSS
        • CSS Color Cheatsheet
        • CSS Common Properties Cheatsheet
        • CSS Flexbox Cheatsheet
        • CSS Grid Cheatsheet
      • HTML
        • HTML Cheatsheet
      • Markdown
        • Markdown Cheatsheet
      • Git
        • Git Cheatsheet
      • Regex
        • Regex Cheatsheet
      • Time
        • Unix Timestamp Cheat
    • About
    • Contact
    • Privacy Policy
    • Terms of Use

        Built with by Hugo

        We use cookies and similar technologies for ads (e.g. Google AdSense) and analytics. By continuing you consent to our Privacy Policy. You can change preferences anytime.