Date () of object  in JavaScript

Date () of object in JavaScript

An independent platform is used to display the date things.

Introduction

JavaScript's Date object allows you to work with dates and times in your code. Date objects encapsulate an integral number that represents milliseconds since midnight at the beginning of January 1, 1970, UTC.

Methods of Date()

new Date()

  • Creates a new Date object with the current date and time.

      const now = new Date();
      console.log(now); // shows current date and time
      // OutPut be like:-
      // 2023-01-17T13:28:49.859Z
    

new Date(milliseconds)

  • Creates a new Date object with the specified number of milliseconds since January 1, 1970, 00:00:00 UTC.

      const birthday = new Date(86400000);
      console.log(birthday); // shows the date for 1 day after Jan 1st, 1970
      // OutPut be like:-
      // 1970-01-02T00:00:00.000Z
    

new Date(dateString)

  • Creates a new Date object from a string, following the ISO 8601 standard (YYYY-MM-DDTHH:mm:ss.sssZ).

      const startOfYear = new Date('2022-01-01T00:00:00Z');
      console.log(startOfYear); // shows the date for Jan 1st, 2022 
      // OutPut be like:-
      // 2022-01-01T00:00:00.000Z
    

getFullYear()

  • Returns the year of the date (four digits for four-digit years).

      const now = new Date();
      console.log(now.getFullYear()); // shows current year
      // OutPut be like:-
      // 2023
    

getMonth()

  • Returns the month of the date (0-11).

      const now = new Date();
      console.log(now.getMonth()); // shows current month
      // OutPut be like:-
      // 0
    

getDate()

  • Returns the day of the month of the date (1-31).

      const now = new Date();
      console.log(now.getDate()); // shows current date
      // OutPut be like:-
      // 17
    

getHours()

  • Returns the hour of the date (0-23).

      const now = new Date();
      console.log(now.getHours()); // shows current hour
      // OutPut be like:-
      // 19
    

getMinutes()

  • Returns the minutes of the date (0-59).

      const now = new Date();
      console.log(now.getMinutes()); // shows current minutes
      // OutPut be like:-
      // 7
    

getSeconds()

  • Returns the seconds of the date (0-59).

      const now = new Date();
      console.log(now.getSeconds()); // shows current seconds
      // OutPut be like:-
      // 31
    

toDateString()

  • Returns the date portion of the Date object in a human-readable format.

      const now = new Date();
      console.log(now.toDateString()); // shows current date in human readable format
      // OutPut be like:-
      // Tue Jan 17 2023
    

toTimeString()

  • Returns the time portion of the Date object in a human-readable format.

      const now = new Date();
      console.log(now.toTimeString()); // shows current time in human readable format
      // OutPut be like:-
      // 19:09:52 GMT+0530 (India Standard Time)
    

toISOString()

  • Returns the date as a string, in ISO format.

      const now = new Date();
      console.log(now.toISOString()); // shows date and time in ISO format
      // OutPut be like:-
      // 2023-01-17T13:40:30.896Z
    

This is something about the Date() in JS.

For like this more code star at: github.com/Prajwal-V-Naik?tab=repositories

For everyday updates connect at: linkedin.com/in/pajju-dev-8431withyou

"Learn to start"

"Happy learning"

Did you find this article valuable?

Support Prajwal V Naik by becoming a sponsor. Any amount is appreciated!