You are currently viewing Date Object in Javascript with example | Advanced Guide

Date Object in Javascript with example | Advanced Guide

Date Object in Javascript with example

In this article, we will see about date object in javascript with example.

Date

Its object provides various methods to store and retrieve date & time information.

There are two different ways in which time is measured, Global(world) time & Local (zonal) time. Global time is the same for all throughout the world, whereas local time differs based on the time zone.

GMT(Greenwich Mean TIme):

Its measures time based on the prime meridian line and divides the globe into time zones. The prime meridian line is the 0-degree longitude line that divides the globe in half; it passes through the Greenwich area, London.

Note: Based on the time zone, the local time differs form GMT by the number of hours ahead or behind.

Create date object

We create date objects using a Date constructor function

Constructor notation

var dateObjectName = new Date();
//Creates a date object using the system current date & time

var dateObjectName = new Date(daate:String);
//Creates a date object using the given date string

var dateObjectName = new Date(year, month, date[, hours[, minuted[, seconds[, ms ]]]]);
//Creates a date object using the given parameters

var dateObjectName = new Date(value:int);
//Creates a date object using the given integer value, representing time in milliseconds

Note : Date object stores date & time based on the number of milliseconds elapsed from Thu

Example codes

//1. current date & time
var date1 = new Date();
document.write(date1);
document.write("<br/>");

// 2. given date string
var date2 = new Date("Sun Mar 25 2018 23:30:08");
document.write(date2);
document.write("<br/>");

//3. given parameters
var date3 = new Date(2018, 2, 25, 23, 37, 9);
document.write(date3);
document.write("<br/>");

//4. given integer value, representing time in milliseconds
var date4 = new Date(2*24*60*60*1000);
document.write(date4.toUTCString());
document.write("<br/>");

Output:

READ ALSO  What is Type Casting in JavaScript | Advanced Guide

date object in javascript with example

  1. First, we create a date object namely “date1” without passing any parameter and print the date1. The output returns the current date & Time.
  2. Then we create the date object namely “date2” with the passing string parameter. Passing String Format is day month date year hour minutes and seconds without the separate commas. Since we pass the string in it, we have to give it in the double quotation. So, the output returns the same dates and times.
  3. Thirdly we have a date3 date object passing an integer parameter. The integer passing format is a year, day, date, hour, minutes, and seconds with separated by a comma. Since we pass the integer in it, we have to give it in normal without any quotes.
  4. Finally, we created the date4 object for a date with passing representing time in milliseconds

 

Leave a Reply