DONATE  
MENU
HTML
JSON
JAVASCRIPT
AJAX

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 » Date Methods
Share Tweet WhatsApp

Date Methods

Previously, you have learnt how to create objects of both the current and any specified date. In this page, you will learn the various methods that enable you to work or manipulate date objects. To help in understanding these methods, we will divide these methods into Getters and Setters. GETTERS The methods classified under getters are those methods that enables us extract particular information from a date object. For example, assuming a date object contains the date, Tue Feb 02 2021 18:56:39 GMT+0100 (WAT), the getters will enable you extract/obtain/get particular information such as day, month, year and etcetera from the date object. This enables you to work with such values. 1. getDate() The getDate() method enables you to get a date's day of the month. Days of the month usually runs from 1 - 31.
<script>
var today = new Date(); //current date object
var day = today.getDate(); //day of the month
alert("The current day of the month is "+day);
</script>

Copy PRACTICE IT

2. getDay() The getDay() method enables you to get a date's day of the week. JavaScript days of the week usually runs from 0(Sunday) - 6(Saturday).
<script>
var dayName;
var today = new Date(); //current date object
var day = today.getDay(); //day of the week
switch (day){
case 0: dayName = "Sunday"; break;
case 1: dayName = "Monday"; break;
case 2: dayName = "Tuesday"; break;
case 3: dayName = "Wednesday"; break;
case 4: dayName = "Thursday"; break;
case 5: dayName = "Friday"; break;
case 6: dayName = "Saturday"; break;
default: dayName = "Unknown";
}

document.write("Today is a beautiful "+dayName);
</script>

Copy PRACTICE IT

3. getFullYear() The getFullYear() method returns the full year of a date object.
<script>
 var d = new Date(-(84000000 * 592)); //the argument is -5114880000
 document.write("The year in "+d+" is "+d.getFullYear());
</script>

Copy PRACTICE IT

4. getHours() Returns the hour of the day in a date. A day usually comprise of 24 hours.
<script>
 var d = new Date();
 alert(d.getHours());
</script>

Copy PRACTICE IT

5. getMilliseconds() Returns the milliseconds of a date object. There are 1000 milliseconds in a second.
<script>
 var d = new Date();
 document.write("The current milliseconds is "+d.getMilliseconds());
</script>

Copy PRACTICE IT

6. getMinutes() Returns the minutes of a date object.
<script>
 var d = new Date();
 document.write("The current minute is "+d.getMinutes());
</script>

Copy PRACTICE IT

7. getMonth() Returns the month of a date object. JavaScript months start from 0(January) to 11(December).
<script>
 var d = new Date();
 document.write("This month is "+d.getMonth());
</script>

Copy PRACTICE IT

8. getSeconds() Returns the seconds of a date object.
<script>
 var d = new Date();
 alert(d.getSeconds());
</script>

Copy PRACTICE IT

9. getTime() Returns the number of milliseconds from January 1st, 1970 to the date of the date object. The value can be negative or positive depending on whether the date was before or after January 1st, 1970.
<script>
var date1 = new Date(1960, 0,); //January 1960 date object
var date2 = new Date(2000, 0); //January 2000 date object
document. write("The number of milliseconds from "+date1+" to January 1970 is <b>"+date1.getTime()+"</b> while that of "+date2+" is <b>"+date2.getTime());
</script>

Copy PRACTICE IT

10. getTimezoneOffset() 11. getUTCDate() The getUTCDate() method is same as the getDate() method but unlike the getDate() method, which gets a date depending on the user's browser timezone, the getUTCDate() returns a date's UTC day of the month. UTC stands for coordinated universal time or universal time coordinated. It is the world standard time from which various timezones in the world are obtain. A timezone date is determined by subtracting a number from the UTC depending on how many other time zones before the time zone. Other UTC methods are: 12. getUTCDay() 13. getUTCFullYear() 14. getUTCHours() 15. getUTCMilliseconds() 16. getUTCMinutes() 17. getUTCMonth() 18. getUTCSeconds()
<script>
var date = new Date(); //current date and time object
var tz_hr = date.getHours();
var utc_hr = date.getUTCHours();
document.write("The world's standard and current hour is "+utc_hr+" while the current hour in your time zone is "+tz_hr);
</script>

Copy PRACTICE IT

19. now() The now() method is a static method of the Date object. This means it shouldn't be called on date objects but rather directly on the Date prototype name. It returns the number of milliseconds since January 1st, 1970 to the current date or moment.
<script>
var d_milli = Date.now(); //Return number of milliseconds from 1970 to now.
var d = new Date(d_milli); //using the milliseconds to create a date object of the current date and time
alert(d);
</script>

Copy PRACTICE IT

20. parse() The parse method is also a static method. It converts a valid date string into the number of milliseconds from January 1st, 1970 to the date string. The difference between this method and the getTime() method is that this method can be used on both valid date strings and date objects unlike the getTime() that must be used on objects only.
<script>
var date = "Wed Feb 03 2021 11:43:15 GMT+0100 (WAT)"; //date string
var date1 = Date(); //date string
var date2 = new Date(1998, 06); //date object
alert(Date.parse(date));
alert(Date.parse(date1));
alert(Date.parse(date2));
</script>

Copy PRACTICE IT

SETTERS Unlike the "GETTER" methods, the methods classified under the SETTER enables you change the individual date values of a date object. For example, changing the day, month, year and etcetera of a date object. 1. setDate() The setDate() method enables you to set or change the date's day of the month. Days of the month usually runs from 1 - 31.
<script>
var date = new Date(); //date object
var prev = date.getDate();
date.setDate(15); //changing day of the month
document.write("Before changing, the day of the month was "+prev+" but now, it is "+date.getDate());
</script>

Copy PRACTICE IT

Other Setter methods are: 2. setDay() 3. setFullYear() 4. setHours() 5. setMilliseconds() 6. setMinutes() 7. setMonth() 8. setSeconds() You can also set or change UTC date, day, full year, hours, milliseconds, minutes, month and seconds of a date object. SUMMARY
  1. The Date object have numerous methods that enables us to manipulate a date object.
  2. These methods mainly gets part of a date (GETTERS) or sets/changes any part(SETTERS).
Take a short quiz on Date Methods.
Ask a question or tell us if Date Methods 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