Timestamps and Date Math
Compute differences and offsets between dates.
Timestamps and Date Math is a free JavaScript Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.
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)Learn JavaScript with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 59
- Lessons
- 200
Frequently Asked Questions
Is the “Timestamps and Date Math” lesson free?
Yes — any 3 lessons of the JavaScript Academy learning path, including “Timestamps and Date Math”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
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