Get Current Date and Time
Get current date and time in various formats
BeginnerTopic: Date/Time Programs
JavaScript Get Current Date and Time Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
// Method 1: Current date and time
const now = new Date();
console.log('Current date:', now);
// Method 2: Get individual components
const date = new Date();
console.log('Year:', date.getFullYear());
console.log('Month:', date.getMonth() + 1); // 0-11, so add 1
console.log('Date:', date.getDate());
console.log('Day:', date.getDay()); // 0-6 (Sunday-Saturday)
console.log('Hours:', date.getHours());
console.log('Minutes:', date.getMinutes());
console.log('Seconds:', date.getSeconds());
console.log('Milliseconds:', date.getMilliseconds());
// Method 3: ISO string
const isoString = new Date().toISOString();
console.log('ISO String:', isoString);
// Method 4: Locale string
const localeString = new Date().toLocaleString();
console.log('Locale String:', localeString);
const localeDateString = new Date().toLocaleDateString();
console.log('Locale Date:', localeDateString);
const localeTimeString = new Date().toLocaleTimeString();
console.log('Locale Time:', localeTimeString);
// Method 5: UTC methods
const utcDate = new Date();
console.log('UTC Year:', utcDate.getUTCFullYear());
console.log('UTC Month:', utcDate.getUTCMonth() + 1);
console.log('UTC Date:', utcDate.getUTCDate());
console.log('UTC Hours:', utcDate.getUTCHours());
// Method 6: Timestamp
const timestamp = Date.now();
console.log('Timestamp:', timestamp);
const timestampFromDate = new Date().getTime();
console.log('Timestamp from Date:', timestampFromDate);
// Method 7: Date string
const dateString = new Date().toString();
console.log('Date String:', dateString);
// Method 8: Custom format
function formatDate(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
console.log('Formatted:', formatDate(new Date()));
// Method 9: Timezone offset
const offset = new Date().getTimezoneOffset();
console.log('Timezone Offset (minutes):', offset);
// Method 10: Date.now() vs new Date()
console.log('Date.now():', Date.now()); // Number
console.log('new Date():', new Date()); // Date objectOutput
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
Understanding Get Current Date and Time
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
Let us now understand every line and the components of the above program.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.