Working with Dates in JavaScript

JavaScript provides a built-in Date object to work with dates and times — including year, month, day, hour, minutes, seconds, and milliseconds.


 How to Create Dates

 1. Current Date and Time

    var currentDate = new Date(); // Creates a date object with the current date and time

 2. Using a Date String

    var date1 = new Date("March 3, 2015");
    var date2 = new Date("2015-03-03 10:30:00");

 3. Using Milliseconds from 1970

    var date1 = new Date(0);      // Jan 1, 1970
    var date2 = new Date(1000);   // 1 second after Jan 1, 1970

4. Using Year, Month, Day, etc.

    var date = new Date(2021, 1, 3); // Month is 0-based: 1 = February
    var date2 = new Date(2021, 1, 3, 10, 30, 45); // With time

Month Index Starts from 0 ? January = 0, February = 1, ..., December = 11


Date Formats

JavaScript supports ISO 8601 date format:

    var date = new Date('2015-02-10T10:12:50.500Z');

Other examples of accepted formats:

    new Date("3/2/2015");        // MM/DD/YYYY
    new Date("3 February, 2015");
    new Date("February 3 2015");

 Some formats may not work consistently across browsers — prefer standard formats like YYYY-MM-DD.


Convert Date Formats

Once you have a date object, you can convert it to different formats:

    var date = new Date('2015-02-10T10:12:50.500Z');

    date.toDateString();         // "Tue Feb 10 2015"
    date.toLocaleDateString();   // "2/10/2015"
    date.toGMTString();          // "Tue, 10 Feb 2015 10:12:50 GMT"
    date.toISOString();          // "2015-02-10T10:12:50.500Z"
    date.toLocaleString();       // "2/10/2015, 10:12:50 AM"
    date.toLocaleTimeString();   // "10:12:50 AM"
    date.toTimeString();         // "10:12:50 GMT+0000"
    date.toUTCString();          // "Tue, 10 Feb 2015 10:12:50 GMT"

Custom Date Format (DD-MM-YYYY)

You can manually format the date:

    var date = new Date('4-1-2015'); // MM-DD-YYYY

    var day = date.getDate();
    var month = date.getMonth() + 1;
    var year = date.getFullYear();

    var dateString =
    (day < 10 ? '0' + day : day) + '-' +
    (month < 10 ? '0' + month : month) + '-' +
    year;

    console.log(dateString); // 01-04-2015

Compare Dates

You can compare two dates using <>==, etc.

    var date1 = new Date('4-1-2015');
    var date2 = new Date('4-2-2015');

    if (date1 > date2) {
    console.log("date1 is later");
    } else if (date1 < date2) {
    console.log("date1 is earlier");
    } else {
    console.log("dates are the same");
    }

 Dates are compared by their numeric timestamp behind the scenes.


 Summary Table

ActionExample Code
Create current datenew Date()
From date stringnew Date("Feb 3, 2015")
From componentsnew Date(2023, 0, 15, 10, 30)
From millisecondsnew Date(1000)
Format to readabledate.toDateString()
Format to ISOdate.toISOString()
Convert to localdate.toLocaleString()
Compare datesif (date1 > date2) { ... }
Custom formatUse getDate()getMonth()getFullYear()








Tips

  • Always use new Date() to create a date object.

  • Prefer ISO format (YYYY-MM-DD) for compatibility.

  • Remember that months are zero-based (Jan = 0).

  • Use libraries like Moment.js or date-fns for advanced formatting and manipulation.