DONATE  
MENU
HTML
JAVASCRIPT
AJAX
JSON

JavaScript Tutorials

INTRODUCTION

» Introduction to JavaScript

» Practice IT Editor

» JavaScript Example

» JavaScript Placement

» JavaScript Syntax

» JavaScript Statements

» JavaScript Input

» JavaScript Output

» JavaScript Comments

» JavaScript Variables

» Variables' Scope

» JavaScript Operators

» JS Expressions & Concatenation

» JavaScript Data Type

» JavaScript Numbers

» Number Object

» JavaScript Strings

» String Object

» JavaScript Booleans

» Boolean Object

» JavaScript Functions

» JavaScript Arrays

» Array Object - Properties

» Array Object - Methods

» JavaScript Objects

» JavaScript Undefined

» JavaScript Nulls

CONDITIONAL TESTING

» If Statement

» If Else Statement

» JavaScript Ternary Operator

» Switch Statement

LOOPS

» For Loops

» While Loops

» Break & Continue

» For...In Loops

» do...while Loop

FUNCTIONS

» JavaScript Functions

» Creating Functions

» Functions' Parameters

» Functions' Arguments

» Function Invocation

» The Return Keyword

» The Arguments Object

» Callback Functions

» Arrow Functions

ARRAYS

» JavaScript Arrays

» Creating Arrays

» Accessing Arrays

» Looping Arrays

» Multi - dimensional Arrays

OBJECTS

» JavaScript Objects

» Creating Objects

» Prototype Definition

» Accessing Objects

» The this Keyword

AJAX

» Introduction to AJAX

JSON

» Introduction to JSON

JAVASCRIPT DATES

» The Date Object

» Date Methods

» Timing (SetInterval & SetTimeout)

MATH OBJECT

» Math Object

» Math Methods

HTML / JS DOM

» DOM Introduction

» Accessing HTML Elements in DOM

» Get And Change Elements' Contents & Values

» Change Styling - DOM CSS

» DOM Attributes

» JavaScript Event Listeners

» DOM Event Listeners

» JS Events

MISCELLANEOUS

» JS Strict Mode

» JavaScript Hoisting

» JavaScript let keyword

» Reserved Keywords

» JS Best Practices

» JavaScript vs ECMAScript

JAVASCRIPT EXAMPLES

» JavaScript Calculator

.. » JavaScript » Javascript Dates » The Date Object
Share Tweet WhatsApp

The Date Object

The JavaScript Date object is just like every other JavaScript object. It is a powerful object that contains properties and methods that enables us work with dates(Years, Months, Days) and times(hours, minutes, seconds, milliseconds) in JavaScript. The Date object is considered one of the non - primitive data types in JavaScript. This is why whenever you output or uses a variable containing a date object, it outputs the date instead of the usual [object Object].
<script>
function myObj(){
}
var obj = new myObj(); //Normal object
var date = new Date(); //Date object
alert(obj); //outputs [object Object]
alert(date); //outputs the date of the date object
</script>

Copy PRACTICE IT

Creating A Date There are various ways to create dates in JavaScript. They are:
Date(); //Current Date String
new Date(); //Current Date Object
new Date(milliseconds); //Date Object From Argument
new Date(date_string); //Date Object From Argument
new Date(year, month, day, hours, minutes, seconds, milliseconds); //Date Object From Argument
Date() To easily create a string containing the current date, we make use of the Date() function. It returns the current date.
<script>
var today = Date(); //current date
alert(today);
</script>

Copy PRACTICE IT

NOTE:
  • It doesn't create a date object instead it creates a string containing the current date. This means the value cannot be manipulated using the properties and methods of JavaScript Date Object.
new Date() You can create a date object from the Date() prototype making use of the new keyword. If no argument is assigned, it creates an object of the current date. A created date object automatically converts to a string whenever outputted or used.
<script>
var today = new Date();
document.write("Today's Date Is: "+today);
</script>

Copy PRACTICE IT

new Date(milliseconds) When creating a date object from the Date() prototype, you can assign a number as an argument to create an object of a specific date. The number which can be positive(+) or negative(-) specifies the number of milliseconds since or before January 1st, 1970 respectively. This means, a number, 86400000 (eighty-six million, four hundred thousand) specifies the date, January 2nd, 1970 while -86400000 is December 31st, 1969. 0 is January 1st, 1970. A single day in milliseconds is 86400000. 1000 milliseconds = 1 second. 60 seconds = 1 minute. 60 minutes = 1 hour. 24 hours = 1 day. 1000 × 60 × 60 × 24 = 86400000.
<script>
var jan_1_1970 = new Date(0);
var jan_2_1970 = new Date(86400000);

document.write("<b>0</b> represents "+jan_1_1970+" while <b>86400000</b> represents "+jan_2_1970);
</script>

Copy PRACTICE IT

new Date(date_string) You can also create an object of a specified date by assigning a string representing the date as argument.
<script>
var date = new Date("January 25, 2021 12:00:00");
alert(date);
</script>

Copy PRACTICE IT

new Date(year, month, day, hours, minutes, seconds, milliseconds) Yet again, you can create the object of a specified date by assigning 2 to 7 numbers as argument. The numbers represents the year, month, day, hours, minutes, seconds, milliseconds of the date.
<script>
var date = new Date(2021, 0, 25, 12, 30, 45, 60);
document.write(date);
</script>

Copy PRACTICE IT

NOTE:
  • JavaScript months starts from 0 to 11 where January is 0 and December is 11.
You can decide to omit any of the last five numbers just as shown below:
  • new Date(2021, 0, 25, 12, 30, 45) - year, month, day, hours, minutes, seconds - milliseconds omitted.
  • new Date(2021, 0, 25, 12, 30) - year, month, day, hours, minutes - seconds and milliseconds omitted.
  • new Date(2021, 0, 25, 12) - year, month, day, hours - minutes, seconds and milliseconds omitted.
  • new Date(2021, 0, 25) - year, month, day - hours, minutes, seconds and milliseconds omitted.
  • new Date(2021, 0) - year, month - day, hours, minutes, seconds and milliseconds omitted.
<script>
var date = new Date(2021, 0, 25);
document.write(date);
</script>

Copy PRACTICE IT

Date Object Properties The JavaScript date object do not necessarily have any defined properties except the default prototype and constructor properties that every defined JS object usually possesses. The prototype property enables you to add a new property or method to the Date prototype while the constructor contains the constructor function that was used in defining the JS Date prototype. Learn about the various methods of the date object in the next page. SUMMARY
  1. The JavaScript Date object enables us to work with dates (years, months, days, hours, minutes, seconds, milliseconds) in JavaScript.
  2. Infact, the Date object is a JavaScript non - primitive data type. This is why whenever you output a date object, it returns a string of its date rather than the usual [object Object].
  3. To create an object of any date, we make use of the new keyword. Nevertheless, you can simply get a string containing the current date by invoking the Date prototype without the new keyword but as a function.
  4. When creating a date object, you can assign an argument, which could be a millisecond(s), date string and others to create a date object of a particular date. In a case where none is assigned, a date object of the current date is created.
  5. The JavaScript date object do not have any defined properties other than those usually possess by all prototypes and objects.
  6. Nevertheless, there exists numerous methods used in manipulating date objects. You will get familiarized with them in the next page.
Take a short quiz on The Date Object.
Ask a question or tell us if The Date Object is easy or not.
cent?

Join our 1,090 subscribers to receive tutorials' updates!

  We won't spam your mail box. We hate it.





StackBeginner 2021 © Joseph Omamode