Timestamps and Date Math
Compute differences and offsets between dates.
Timestamps and Date Math is a free JavaScript Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Timestamps
Internally a date is stored as a timestamp: the number of milliseconds since the Unix epoch, midnight on January 1, 1970, UTC.
const d = new Date("1970-01-01T00:00:00Z")
console.log(d.getTime())Date.now
Date.now returns the current timestamp in milliseconds without creating a Date object, which is efficient for measuring time.
const t = Date.now()
console.log(typeof t)
console.log(t > 0)getTime
The getTime method returns a date object as its millisecond timestamp, useful for comparisons and arithmetic.
const d = new Date(2024, 0, 1)
console.log(typeof d.getTime())Comparing Dates
Compare two dates by comparing their timestamps. The earlier date has the smaller number.
const a = new Date(2024, 0, 1)
const b = new Date(2024, 5, 1)
console.log(a.getTime() < b.getTime())Difference in Milliseconds
Subtracting one date from another coerces both to timestamps and yields the difference in milliseconds.
const a = new Date(2024, 0, 1)
const b = new Date(2024, 0, 2)
console.log(b - a)Converting to Days
Divide a millisecond difference by the number of milliseconds in a day to get the gap in days.
const a = new Date(2024, 0, 1)
const b = new Date(2024, 0, 11)
const ms = b - a
const days = ms / (1000 * 60 * 60 * 24)
console.log(days)Adding Time
To add time, convert to a timestamp, add the millisecond amount, and create a new date from the result.
const start = new Date(2024, 0, 1)
const oneDay = 1000 * 60 * 60 * 24
const next = new Date(start.getTime() + oneDay)
console.log(next.getDate())setDate for Calendar Math
The setDate method adjusts the day of the month and correctly rolls over into the next month when needed.
const d = new Date(2024, 0, 31)
d.setDate(d.getDate() + 1)
console.log(d.getMonth())
console.log(d.getDate())Measuring Elapsed Time
Capture Date.now before and after an operation and subtract to measure how long it took in milliseconds.
const start = Date.now()
let sum = 0
for (let i = 0; i < 1000; i++) { sum += i }
const elapsed = Date.now() - start
console.log(elapsed >= 0)Timestamp to Date
Passing a numeric timestamp to the Date constructor reconstructs the original moment.
const ts = 1704067200000
const d = new Date(ts)
console.log(d.getUTCFullYear())Seconds Versus Milliseconds
JavaScript timestamps are in milliseconds, but many APIs use seconds. Multiply or divide by 1000 to convert between them.
const seconds = 1704067200
const d = new Date(seconds * 1000)
console.log(d.getUTCFullYear())Quick Check
Test your timestamp knowledge.
Recap
Recap: Dates are millisecond timestamps under the hood.
- Date.now gives the current timestamp
- Subtract dates for a millisecond difference
- Divide by day milliseconds for day counts
- JavaScript uses milliseconds, not seconds
const a = new Date(2024, 0, 1)
const b = new Date(2024, 0, 2)
console.log(b - a)Frequently asked questions
Is the “Timestamps and Date Math” lesson free?
Yes — the full text of “Timestamps and Date Math” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Timestamps and Date Math”?
Compute differences and offsets between dates. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Timestamps and Date Math” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Creating and Reading Dates
- Timestamps and Date Math
- Formatting Dates
- Time Zones and UTC