Generate Random Timestamps in JavaScript
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: JavaScript
What: Generate random Unix timestamps in JavaScript using Date.now() and Math.random(). Covers current time, past/future timestamps, and formatting for testing.
Try it: Use our interactive Timestamps generator or integrate this code into your JavaScript application.
Generate random Unix timestamps in JavaScript using Date.now() and Math.random(). Covers current time, past/future timestamps, and formatting for testing. Looking for other languages? Check our code examples in PHP , Python , Java , C# , C++ , Ruby and Go or use our interactive web generator.
JavaScript Code Example
// Generate current Unix timestamp (milliseconds)
function currentTimestamp() {
return Date.now();
}
// Generate current Unix timestamp (seconds)
function currentTimestampSeconds() {
return Math.floor(Date.now() / 1000);
}
// Generate random timestamp between two dates
function randomTimestamp(startDate, endDate) {
const start = new Date(startDate).getTime();
const end = new Date(endDate).getTime();
return Math.floor(Math.random() * (end - start + 1)) + start;
}
// Generate random past timestamp (within N days)
function randomPastTimestamp(daysAgo = 365) {
const now = Date.now();
const milliseconds = daysAgo * 86400000; // 86400000 ms in a day
return Math.floor(Math.random() * milliseconds) + (now - milliseconds);
}
// Generate random future timestamp (within N days)
function randomFutureTimestamp(daysAhead = 365) {
const now = Date.now();
const milliseconds = daysAhead * 86400000;
return Math.floor(Math.random() * milliseconds) + now;
}
// Convert timestamp to readable format
function formatTimestamp(timestamp, locale = "en-US") {
return new Date(timestamp).toLocaleString(locale);
}
// Generate multiple random timestamps
function generateTimestamps(count = 10, startDate = "2020-01-01", endDate = "2024-12-31") {
return Array.from({ length: count }, () => randomTimestamp(startDate, endDate));
}
console.log("Current (ms):", currentTimestamp());
console.log("Current (s):", currentTimestampSeconds());
console.log("Random:", randomTimestamp("2023-01-01", "2024-12-31"));
console.log("Past:", randomPastTimestamp(30), "(" + formatTimestamp(randomPastTimestamp(30)) + ")");
console.log("Future:", randomFutureTimestamp(30), "(" + formatTimestamp(randomFutureTimestamp(30)) + ")");
console.log("Batch:", generateTimestamps(3).join(", "));
[EXPLANATION]
JavaScript Date.now() returns current timestamp in milliseconds (not seconds like Unix). new Date(dateString).getTime() converts dates to milliseconds. Math.random() generates random values within ranges. Math.floor() converts to integers. 86400000 is milliseconds per day. toLocaleString() formats for display. Divide by 1000 to convert JavaScript timestamps to Unix seconds for backend APIs.
Expected Output
Current (ms): 1703001234567 Current (s): 1703001234 Random: 1698765432000 Past: 1700445678000 (11/19/2023, 2:27:58 PM) Future: 1706123456000 (1/24/2024, 6:10:56 PM) Batch: 1680123456000, 1685432100000, 1692345678000
Common Use Cases
- Generate timestamps for React/Vue component testing
- Create event timestamps for frontend calendar applications
- Test timestamp-based caching in browser applications
- Generate log timestamps for Node.js server testing
- Create API request timestamps for rate limiting tests
Important Notes
- JavaScript timestamps are in milliseconds, not seconds (divide by 1000 for Unix)
-
Date.now()is faster thannew Date().getTime() -
toISOString()returns UTC format: "2024-01-01T12:00:00.000Z" -
toLocaleString()respects user timezone and locale preferences -
Use
Number.isSafeInteger()to check for safe integer range
Try Our Interactive Generator
Don't want to write code? Use our free web-based Timestamps generator with instant results.
TRY TIMESTAMPS GENERATOR →Other Programming Languages
View Timestamps generation code examples in PHP
View Timestamps generation code examples in Python
View Timestamps generation code examples in Java
View Timestamps generation code examples in C#
View Timestamps generation code examples in C++
View Timestamps generation code examples in Ruby
View Timestamps generation code examples in Go