JavaScript Date Objects

Date Objects

The Date object is a datatype built into the JavaScript language. Date objects are created with the new Date( ) as shown below.

Once a Date object is created, a number of methods allow you to operate on it. Most methods simply allow you to get and set the year, month, day, hour, minute, second, and millisecond fields of the object, using either local time or UTC (universal, or GMT) time.

The ECMAScript standard requires the Date object to be able to represent any date and time, to millisecond precision, within 100 million days before or after 1/1/1970. This is a range of plus or minus 273,785 years, so JavaScript can represent date and time till the year 275755.

Here is a description of the parameters −

  • No Argument − With no arguments, the Date() constructor creates a Date object set to the current date and time.
  • milliseconds − When one numeric argument is passed, it is taken as the internal numeric representation of the date in milliseconds, as returned by the getTime() method. For example, passing the argument 5000 creates a date that represents five seconds past midnight on 1/1/70.
  • datestring − When one string argument is passed, it is a string representation of a date, in the format accepted by the Date.parse() method
  • 7 agruments − To use the last form of the constructor shown above. Here is a description of each argument −
  • year − Integer value representing the year. For compatibility (in order to avoid the Y2K problem), you should always specify the year in full; use 1998, rather than 98.
  • month − Integer value representing the month, beginning with 0 for January to 11 for December.
  • date − Integer value representing the day of the month.
  • hour − Integer value representing the hour of the day (24-hour scale).
  • minute − Integer value representing the minute segment of a time reading.
  • second − Integer value representing the second segment of a time reading.
  • millisecond − Integer value representing the millisecond segment of a time reading.

Creating Date Objects

Date objects are created with the new Date() constructor.There are 4 ways to create a new date object:

  • new Date()
  • new Date(year, month, day, hours, minutes, seconds, milliseconds)
  • new Date(milliseconds)
  • new Date(date string)

new Date()

Tnew Date() creates a new date object with the current date and time. Date objects are static. The computer time is ticking, but date objects are not.

      
                    var d = new Date();
                    

new Date(year, month, ...)

Tnew Date(year, month, ...) creates a new date object with a specified date and time. 7 numbers specify year, month, day, hour, minute, second, and millisecond (in that order):

      
                    var d = new Date(2018, 11, 24, 10, 33, 30, 0);
                    

6 numbers specify year, month, day, hour, minute, second:

      
                    var d = new Date(2018, 11, 24, 10, 33, 30);
                    

You cannot omit month. If you supply only one parameter it will be treated as milliseconds.

      
                    var d = new Date(2018);
                    

new Date(dateString)

new Date(dateString) creates a new date object from a date string: >

You cannot omit month. If you supply only one parameter it will be treated as milliseconds.

      
                    var d = new Date("October 13, 2014 11:13:00");
                    

new Date(milliseconds)

new Date(milliseconds) creates a new date object as zero time plus milliseconds:

      
                    var pi = 3.14;
var person = "John Doe";
var answer = 'Yes I am!';
                    

Date Methods

When a Date object is created, a number of methods allow you to operate on it. Date methods allow you to get and set the year, month, day, hour, minute, second, and millisecond of date objects, using either local time or UTC (universal, or GMT) time.

JavaScript will (by default) output dates in full text string format. When you display a date object in HTML, it is automatically converted to a string, with the toString() method.

      
                    d = new Date();
document.getElementById("demo").innerHTML = d;
                    

Same as

      
                    d = new Date();
document.getElementById("demo").innerHTML = d.toString();
                    

The toUTCString() method converts a date to a UTC string (a date display standard).

      
                    var d = new Date();
document.getElementById("demo").innerHTML = d.toUTCString();
                    

The toDateString() method converts a date to a more readable format:

      
                    var d = new Date();
document.getElementById("demo").innerHTML = d.toDateString();
                    
      
                    var d = new Date();
document.getElementById("demo").innerHTML = d.toISOString();
                    

Dess App

DessApp is an Integrated E-learning Education, Interactive and User-friendly features, smarter options and redefining your school costs effectively and efficiently.

View
1 1