Date object in JavaScript

Date object is a data type built into JavaScript Language. Date objects are created with new Date () as below:

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

The ECMAScript standard requires that the Date object can represent any date and time, accurate to milliseconds, within 100 million days before or after January 1, 1970. This is a sequence of plus or minus 273,785 years, so JavaScript can represent the date and time until 275755.

Syntax

You can use the following syntax to create a Date object using the Date () constructor.

 new Date ( ) new Date ( milliseconds ) new Date ( datestring ) new Date ( year , month , date [, hour , minute , second , millisecond ]) 

Note - Parameters in parentheses are always optional.

The following is a description of the parameters according to each syntax:

No parameters - With no parameters, the Date () constructor creates a Date object set to the current time and date.

milliseconds - When a numeric parameter is passed, it receives the representation of the internal numerical value of the day in milliseconds, when returned by the getTime () method. For example, passing parameter 5000 creates a day that represents 5 seconds before midnight on January 1, 1970.

datestring - When a string parameter is passed, it is a string representation of a date, in a format accepted by the Date.parse () method .

7 agruments - To use the last form of the constructor shown above. Here we describe each parameter one:

year - Integer value representing year. For compatibility (avoid the Y2K problem), you should always specify the full year, using 1998 instead of 98.

month - Integer value representing the month, starting with 0 for January and 11 for December.

date - Integer value representing the day of the month.

hour - Integer value representing the time of day (24 hours).

minute - Integer value representing minute.

second - Integer value represents the second.

millisecond - Integer value representing milliseconds.

Properties of Date

Below is a list of the properties of the Date object along with their description:

Properties Description

constructor

Define the function that creates an object prototype.

prototype

This property allows you to add properties and methods to an object.

Methods of Date

Below is a list of methods of the Date object along with their description:

Method Description

Date ()

Returns the date and time of today.

getDate ()

Returns the day of the month for the specified date according to Local time

getDay ()

Returns the day of the week for the specified date according to Local time

getFullYear ()

Returns the year of the given date according to Local time

getHours ()

Returns the hour of the given day according to Local time

getMilliseconds ()

Returns the millisecond of a given date according to Local time

getMinutes ()

Returns the minute of the given date according to Local time

getMonth ()

Returns the month of the given date according to Local time

getSeconds ()

Returns the second of the given date according to Local time

getTime ()

Returns the numerical value of the given day when the number of milliseconds is from January 1970, 00:00:00 UTC.

getTimezoneOffset ()

Returns the Time-zone Offset in minutes for the current Locale.

getUTCDate ()

Returns the day of the month of the given date according to Universal time

getUTCDay ()

Returns the day of the week of the given date according to Universal time

getUTCFullYear ()

Returns the year of the given date according to Universal time

getUTCHours ()

Returns the hour of the given date according to Universal time

getUTCMilliseconds ()

Returns milliseconds of the given date according to Universal time

getUTCMinutes ()

Returns the minute of the given date according to Universal time

getUTCMonth ()

Returns the month of the given date according to Universal time

getUTCSeconds ()

Returns the second of the given date according to Universal time

getYear ()

Old method - Returns the year of the given date according to Local time. You use getFullYear instead.

setDate ()

Returns the date of the month for the specified date according to Local time.

setFullYear ()

Set the full year for the given date according to Local time.

setHours ()

Set the time for the given day according to Local time.

setMilliseconds ()

Set milliseconds for a given date according to Local time.

setMinutes ()

Set the minutes for the given day according to Local time.

setMonth ()

Set the month for the given day according to Local time.

setSeconds ()

Set the seconds for the given day according to Local time.

setTime ()

Set the time the Date object is represented by the number of milliseconds from January, 1970, 00:00:00 UTC.

setUTCDate ()

Set the date of the month for the given date according to Universal time

setUTCFullYear ()

Set the full year for the given date according to Universal time

setUTCHours ()

Set the time for the given date according to Universal time

setUTCMilliseconds ()

Set milliseconds for the given date according to Universal time

setUTCMinutes ()

Set minutes for the given date according to Universal time

setUTCMonth ()

Set the month for the given date according to Universal time

setUTCSeconds ()

Set the seconds for the given date according to Universal time

setYear ()

Old method - Set the year for the given date according to Local time. You use setFullYear instead.

toDateString ()

Returns a series of days that humans read.

toGMTString ()

Old method - Convert 1 day to 1 string using Internet GMT conventions. You use toUTCString instead.

toLocaleDateString ()

Returns the date as a string, using the current Locale convention

toLocaleFormat ()

Transform date into string, using format string.

toLocaleString ()

Transform day into string, using current Locale conventions.

toLocaleTimeString ()

Returns the time of a day in string form, using the current Locale convention.

toSource ()

Returns a string representing the source for an equivalent Date object, you can use this value to create a new object.

toString ()

Returns a string representing the given Date object.

toTimeString ()

Returns the time of the Date object in human readable form.

toUTCString ()

Transform a day into a string, using Universal time convention.

valueOf ()

Returns the initial value of a Date object.

Static methods (Static Method) of Date

In addition to the methods listed above, the Date object also defines two static methods (Static Method). These methods are called via the Date () constructor itself.

Method Description

Date.parse ()

Analyze a string representation of a date and time and return the representation of the internal millisecond of that day.

Date.UTC ()

Returns the millisecond representation of the given date and time UTC.

According to Tutorialspoint

Previous article: Array (Array) in JavaScript

Next lesson: Math object in JavaScript

4 ★ | 1 Vote

May be interested

  • How to turn on and turn off Javascript on Firefox, ChromeHow to turn on and turn off Javascript on Firefox, Chrome
    javascript is an object-based scripting language that often creates eye-catching effects on websites.however, if you need to turn on or off javascript, the following article will guide you to do that.
  • Regular Expression and RegExp in JavaScriptRegular Expression and RegExp in JavaScript
    a regular expression is an object that describes a pattern of characters.
  • Boolean objects in JavaScriptBoolean objects in JavaScript
    boolean objects represent two values, either true or false. if the value parameter is omitted either 0, -0, null, false, nan, undefined, or an empty string (), the object has an initial value of false.
  • Array (Array) in JavaScriptArray (Array) in JavaScript
    array object - array helps you store multiple values ​​in a single variable. it stores a set of fixed-size ranges of elements in the same type (type). an array is used to store a data set, but it is often more useful to think of an array as a collection of variables in the same type.
  • Math object in JavaScriptMath object in JavaScript
    the math object gives you properties and methods for constants and mathematical functions. unlike other global objects, math is not a constructor. all properties and methods of math are static (static) and can be called using math as an object without having to create it.
  • What is JavaScript? Can the Internet exist without JavaScript?What is JavaScript?  Can the Internet exist without JavaScript?
    not everyone knows what javascript is and how it works. the long and fascinating development history of javascript as well as what we can do with javascript is still unknown.
  • What is Currying in Javascript? How to use Currying in JavaScriptWhat is Currying in Javascript? How to use Currying in JavaScript
    the currying feature in javascript can help you keep your code tidy and give you a new way of seeing how functions work. currying is ideal when you want to break complex logic into smaller, manageable, and self-contained pieces of code.
  • Things to know about 'this' in JavaScriptThings to know about 'this' in JavaScript
    are you having trouble understanding the keyword 'this' in javascript ? then please read what you need to know about 'this' in javascript below.
  • Udemy's top 5 JavaScript coursesUdemy's top 5 JavaScript courses
    a programming language that runs on any computer in the world. a language does not need any special software to run. a language ranked among the top in the world.
  • Top site with many good JavaScript exercises to practiceTop site with many good JavaScript exercises to practice
    many people, after taking javascript courses and being equipped with some knowledge of the language they pursue, are eager to improve and cultivate these learned skills. so this article will give you a list of the top 3 websites to actually make javascript.