× Introduction Setup Environment Building blocks Built-in functions Data Types Strings Operators Conditional statements Loop statements Functions Arrays Understaing Objects Date Object Number Object Math Object String Object Window Location Navigator History DOM Basics Forms
   Programs
Basic Control Loops Functions Arrays Examples Projects Quick Ref.
   Exercises
Variables Data Types Operators Decision Loops Reeborg's World



Java script Date Object

In this tutorial you will learn how to work with date and time in JavaScript.

JavaScript provides the Date object for manipulating date and time.The Date object provides a number of methods for getting or setting specific information about the date and time.

Because client-side JavaScript programs run on a browser, the Date object returns times and dates that are local to the browser, not the server. Of course, if the computer is not set to the correct time, then the Date object won’t produce the expected results.

Creating dates

Before we start working with the date and time, we need to create a Date object.You can simply declare a new Date object without initializing its value. In this case, the date and time value will be set to the current date and time on the user's device on which the script is run.

Example
var currentDateTime=newDate();
document.write(currentDateTime);

JavaScript can also convert many string formats to a date. There are five formatsthat can be passed as arguments when creating a Date object.

Example
new Date("Month dd, yyyyhh:mm:ss");
new Date("Month dd, yyyy");
new Date(yy,mm,dd,hh,mm,ss);
new Date(yy,mm,dd);
new Date(milliseconds);

letcurrentDateTime = new Date(2022,0,18,10,32,10,40);
document.write(currentDateTime);       //Tue Jan 18 2022 10:32:10 GMT+0530 (India Standard Time)


letcurrentDateTime = new Date("15 April 2022");
document.write(currentDateTime);    // Fri Apr 15 2022 00:00:00 GMT+0530 (India Standard Time)

letcurrentDateTime = new Date(1670869744745);
document.write(currentDateTime);   //Mon Dec 12 2022 23:59:04 GMT+0530 (India Standard Time)

Built-in method, now(),returns the current date and timerepresented in seconds since January 1st 1970.

Example
letcurrentDateTime= Date.now();
document.write(currentDateTime);   //1670869969753

Getting the Year, Month and Date

The Date object provides several methods such as getFullYear(), getMonth(), getDay(), etc. that you can use to extract the specific date components from the Date object, such as year, day of month, day of week, etc. respectively.

Example
var d =newDate();
// Extracting date part
alert(d.getDate());     // Display the day of the month
alert(d.getDay());      // Display the number of days into the week (0-6)
alert(d.getMonth());    // Display the number of months into the year (0-11)
alert(d.getFullYear());  // Display the full year (four digits

Getting the Hours, Minutes, Seconds, and Milliseconds

Similarly, the Date object provides methods like getHours(), getMinutes(), getSeconds(), getTimezoneOffset() etc. to extract the time components from the Date object.

Example
var d =newDate();
// Extracting time part 
alert(d.getHours());    // Display the number of hours into the day (0-23)
alert(d.getMinutes());   // Display the number of minutes into the hour (0-59)
alert(d.getSeconds());   // Display the seconds into the minute (0-59)
alert(d.getMilliseconds());   // Display the number of milliseconds into second (0-999)
alert(d.getTime());       // Display the number of milliseconds since 1/1/1970
alert(d.getTimezoneOffset());  // Display the time-zone offset (from Greenwich Mean Time) in minutes

Setting the Date and Time Values

In addition to retrieving date and time values, you can also set or modify these values using the JavaScript. This is most often used in program where you have to change the value of a date object from one particular date or time to another.

Example
var d =newDate();
d.setFullYear(d.getFullYear()+2);
document.write(d);    // Display future date
d.setMonth(0);      // Sets month to 0, January
document.write(d);
d.setHours(8);
d.setMinutes(30);
d.setSeconds(45);
d.setMilliseconds(600);
document.write(d);

Creating the Date and Time Strings

The JavaScript Date object provides several methods, such as toDateString(), toLocaleDateString(), etc. to generate date strings in different formats.

Example
var d =newDate();
alert(d.toDateString());       // Display an abbreviated date string
alert(d.toLocaleDateString());    // Display a localized date string
alert(d.toISOString());      // Display the ISO standardized date string
alert(d.toUTCString());     // Display a date string converted to UTC time
alert(d.toString());        // Display the full date string with local time zone
alert(d.toTimeString());      // Display the time portion of the date
alert(d.toLocaleTimeString());    // Display a localized time string