Get Current Date and Time

Get current date and time in various formats

BeginnerTopic: Date/Time Programs
Back

JavaScript Get Current Date and Time Program

This program helps you to learn the fundamental structure and syntax of JavaScript programming.

Try This Code
// 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 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

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.

Practical Learning Notes for Get Current Date and Time

This JavaScript program is part of the "Date/Time Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents