← Back to Blog
2026-03-132 min readby DevUtilz

Timestamp Converter and Epoch Time

TimestampDateTimeTools

Timestamp Converter and Epoch Time

Unix timestamps are used everywhere in programming. Learn how to work with them effectively.

What is Unix Timestamp?

Unix timestamp = seconds since January 1, 1970 (UTC)

Example: 1704067200 = January 1, 2024 at midnight UTC

Converting in JavaScript

// Current timestamp
const now = Math.floor(Date.now() / 1000);

// Timestamp to Date
const date = new Date(1704067200 * 1000);

// Date to timestamp
const timestamp = Math.floor(new Date('2024-01-01').getTime() / 1000);

Common Timestamp Formats

| Format | Example | |--------|---------| | Unix (seconds) | 1704067200 | | Unix (milliseconds) | 1704067200000 | | ISO 8601 | 2024-01-01T00:00:00Z | | UTC | Mon, 01 Jan 2024 00:00:00 GMT |

Time Zones

// Convert timestamp to specific timezone
const date = new Date(timestamp * 1000);
console.log(date.toLocaleString('en-US', { timeZone: 'America/New_York' }));

// Using Intl (modern approach)
const formatter = new Intl.DateTimeFormat('en-US', {
  timeZone: 'America/New_York',
  dateStyle: 'long',
  timeStyle: 'long'
});
console.log(formatter.format(date));

Useful Timestamp Operations

// Add 24 hours
const tomorrow = timestamp + (24 * 60 * 60);

// Get start of day
const startOfDay = Math.floor(new Date().setHours(0,0,0,0) / 1000);

// Get end of day
const endOfDay = Math.floor(new Date().setHours(23,59,59,999) / 1000);

Common Use Cases

  1. Database storage - Store as integer for efficiency
  2. API responses - Often use ISO 8601
  3. File names - Include timestamp for sorting
  4. Cache keys - Add timestamp for expiration

Handling Time Zones

// Best practice: Store in UTC, display in local
const utcTimestamp = 1704067200;
const localDate = new Date(utcTimestamp * 1000);

// Display to user in their timezone
const userTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
console.log(`Timezone: ${userTimezone}`);

Quick Reference

| Description | Unix (seconds) | |-------------|---------------| | 1 minute | 60 | | 1 hour | 3600 | | 1 day | 86400 | | 1 week | 604800 | | 1 year (365 days) | 31536000 |

Conclusion

Always store timestamps in UTC. Convert to local time only when displaying to users. Use standard libraries for timezone handling.