Javascript - Date Get Methods

Introduction

Get methods are used for getting a part of a date.

Method Description
getDate() Get the day as a number (1-31)
getDay() Get the weekday as a number (0-6)
getFullYear() Get the four digit year (yyyy)
getHours()Get the hour (0-23)
getMilliseconds() Get the milliseconds (0-999)
getMinutes() Get the minutes (0-59)
getMonth()Get the month (0-11)
getSeconds() Get the seconds (0-59)
getTime() Get the time (milliseconds since January 1, 1970)

Demo

//getTime() returns the number of milliseconds since January 1, 1970:
//The internal clock in JavaScript starts at midnight January 1, 1970.
//The getTime() function returns the number of milliseconds since then:
var d = new Date();
console.log(d.getTime());/*from  w  ww  .ja  v a  2 s  .  co m*/

//getFullYear() Method
//getFullYear() returns the year of a date as a four digit number:
//The getFullYear() method returns the full year of a date
console.log(d.getFullYear());

//The getDay() Method
//getDay() returns the weekday as a number (0-6):
//The getDay() method returns the weekday as a number:
console.log(d.getDay());

Result

In JavaScript, the first day of the week (0) means "Sunday"

You can use an array of names, and getDay() to return the weekday as a name:

Demo

//You can use an array to display the name of the weekday:
var d = new Date();
var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
console.log(days[d.getDay()]);//from  w  w w. j  a va  2 s.  c  om

Result

Related Topic