본문으로 건너뛰기

Date Object

Date Object

JavaScript uses the Date object to handle date and time information.


Creating a Date Object

In JavaScript, you can create a new date object using new Date().

Example:

let currentTime = new Date();
console.log(currentTime); // Outputs the current date and time information

Various Uses of the Date Object

  1. Setting a specific date and time: When creating a Date object, you can set a specific date and time.

    let myBirthday = new Date('1995-07-20');

  1. Getting year, month, day, hour, minute, and second: You can retrieve specific information from a Date object.

    let year = currentTime.getFullYear(); // Year
    let month = currentTime.getMonth() + 1; // Month (0-indexed so add 1)
    let date = currentTime.getDate(); // Day
    let hours = currentTime.getHours(); // Hour
    let minutes = currentTime.getMinutes(); // Minute
    let seconds = currentTime.getSeconds(); // Second

  1. Changing the date and time: It is also possible to change the date or time information of a Date object.

    myBirthday.setFullYear(2000); // Change the year to 2000