Get Current Date and Time in JavaScript
Get the current date and time in JavaScript with new Date(), getters, ISO, locale and UTC methods, timestamps and a custom format. Explanation and output.
The sample output was captured with the clock set to 15 January 2024, 10:30:45 UTC. Your run will show your own date, time and time zone.
1// Method 1: Current date and time2const now = new Date();3console.log('Current date:', now);45// Method 2: Get individual components6const date = new Date();7console.log('Year:', date.getFullYear());8console.log('Month:', date.getMonth() + 1); // 0-11, so add 19console.log('Date:', date.getDate());10console.log('Day:', date.getDay()); // 0-6 (Sunday-Saturday)11console.log('Hours:', date.getHours());12console.log('Minutes:', date.getMinutes());13console.log('Seconds:', date.getSeconds());14console.log('Milliseconds:', date.getMilliseconds());1516// Method 3: ISO string17const isoString = new Date().toISOString();18console.log('ISO String:', isoString);1920// Method 4: Locale string21const localeString = new Date().toLocaleString();22console.log('Locale String:', localeString);2324const localeDateString = new Date().toLocaleDateString();25console.log('Locale Date:', localeDateString);2627const localeTimeString = new Date().toLocaleTimeString();28console.log('Locale Time:', localeTimeString);2930// Method 5: UTC methods31const utcDate = new Date();32console.log('UTC Year:', utcDate.getUTCFullYear());33console.log('UTC Month:', utcDate.getUTCMonth() + 1);34console.log('UTC Date:', utcDate.getUTCDate());35console.log('UTC Hours:', utcDate.getUTCHours());3637// Method 6: Timestamp38const timestamp = Date.now();39console.log('Timestamp:', timestamp);4041const timestampFromDate = new Date().getTime();42console.log('Timestamp from Date:', timestampFromDate);4344// Method 7: Date string45const dateString = new Date().toString();46console.log('Date String:', dateString);4748// Method 8: Custom format49function formatDate(date) {50 const year = date.getFullYear();51 const month = String(date.getMonth() + 1).padStart(2, '0');52 const day = String(date.getDate()).padStart(2, '0');53 const hours = String(date.getHours()).padStart(2, '0');54 const minutes = String(date.getMinutes()).padStart(2, '0');55 const seconds = String(date.getSeconds()).padStart(2, '0');5657 return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;58}5960console.log('Formatted:', formatDate(new Date()));6162// Method 9: Timezone offset63const offset = new Date().getTimezoneOffset();64console.log('Timezone Offset (minutes):', offset);6566// Method 10: Date.now() vs new Date()67console.log('Date.now():', Date.now()); // Number68console.log('new Date():', new Date()); // Date object
Sample Output
Day: 1
getDay returns the weekday. 15 January 2024 was a Monday, which is 1.
Timezone Offset (minutes): 0
This run used UTC. In India you would see -330.
In this one
- How to create the current date with new Date() and Date.now()
- Why getMonth starts at 0 and getDay means weekday
- The difference between ISO, locale and UTC output
- How to build your own date format with padStart
How it works
- 1
new Date() creates the current date and prints it.
- 2
Getters print year, month + 1, date, weekday and time parts.
- 3
toISOString and locale methods print different text formats.
- 4
UTC getters print the UTC parts.
- 5
Date.now() and getTime() print the timestamp.
- 6
formatDate builds YYYY-MM-DD HH:mm:ss and the offset is printed.
Edge cases
Forgetting + 1 on the month
January prints as 0.
getMonth is 0-indexed. Add 1 when you show it.
getDay vs getDate
getDay() returns 1 on 15 January 2024.
getDay is the weekday (Monday). Use getDate for 15.
Different machines
Hours differ between your laptop and a server.
Local getters use the machine time zone. Store dates in UTC.
What's going on
Almost every app needs the current date or time. A school app stamps when attendance was marked. A chat app shows when a message was sent. In JavaScript you get it with the Date object.
new Date() with no arguments creates a Date for right now. You can also create one from a timestamp with new Date(timestamp), or for a specific day with new Date(year, month, day). Date.now() skips the object and returns the current timestamp, a number.
A timestamp is the number of milliseconds since 1 January 1970, UTC. getTime() returns the same number from a Date object.
Method 2 reads parts of the date with getters: getFullYear, getMonth, getDate, getHours, getMinutes, getSeconds and getMilliseconds. Two of them surprise everyone. getMonth is 0-indexed, so January is 0 and December is 11. That is why the code adds 1. getDay is the day of the week, where 0 is Sunday and 6 is Saturday. Do not confuse getDay with getDate, which is the day of the month.
toISOString gives a standard text format like 2024-01-15T10:30:45.123Z. The Z means UTC. Use it for saving and sending dates.
The locale methods format a date for people. toLocaleString shows date and time, toLocaleDateString only the date, and toLocaleTimeString only the time. The style depends on the user's language settings.
UTC methods like getUTCFullYear, getUTCMonth and getUTCHours read the date in UTC instead of the local time zone. UTC is the world reference time.
Method 8 builds a custom format with padStart, which adds a leading zero so 5 becomes 05. Method 9 reads getTimezoneOffset, the minutes between local time and UTC.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does getMonth() return for March?
Pick an answer to check it.
Question 2
What does getDay() return for a Sunday?
Pick an answer to check it.
Questions people ask
What is the difference between Date.now() and new Date()?
Date.now() returns a number. new Date() returns a Date object with methods.
Why does the console show a Z at the end?
Node prints dates in ISO format, and Z means UTC.
What does a timezone offset of -330 mean?
Local time is 330 minutes ahead of UTC, as in India (UTC+5:30). The sign is flipped.