Get Current Date and Time in JavaScript
Get current date and time in various formats
BeginnerDate/Time ProgramsExample 1 of 10
get-current-date-and-time.js
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
Output
Current date: 2024-01-15T10:30:45.123Z Year: 2024 Month: 1 Date: 15 Day: 1 Hours: 10 Minutes: 30 Seconds: 45 Milliseconds: 123 ISO String: 2024-01-15T10:30:45.123Z Locale String: 1/15/2024, 10:30:45 AM Locale Date: 1/15/2024 Locale Time: 10:30:45 AM UTC Year: 2024 UTC Month: 1 UTC Date: 15 UTC Hours: 10 Timestamp: 1705315845123 Timestamp from Date: 1705315845123 Date String: Mon Jan 15 2024 10:30:45 GMT+0000 (UTC) Formatted: 2024-01-15 10:30:45 Timezone Offset (minutes): 0 Date.now(): 1705315845123 new Date(): 2024-01-15T10:30:45.123Z
What's going on
Date objects represent dates and times.
Date Creation
new Date(): Current date/time
new Date(timestamp): From timestamp
new Date(year, month, day): Specific date
Date.now(): Current timestamp
Date Methods
getFullYear/getMonth/getDate
getHours/getMinutes/getSeconds
getTime: Timestamp
toISOString: ISO format
Locale Methods
toLocaleString: Local format
toLocaleDateString: Date only
toLocaleTimeString: Time only
UTC Methods
getUTCFullYear/getUTCMonth
getUTCHours/getUTCMinutes
Work with UTC timezone
Important Notes
Month is 0-indexed (0-11)
Day of week: 0=Sunday, 6=Saturday
getTime() returns milliseconds