Home  >  Article  >  Web Front-end  >  What is the method to get date in javascript

What is the method to get date in javascript

coldplay.xixi
coldplay.xixiOriginal
2021-04-15 11:39:145059browse

How to get the date in javascript: 1. Use the [getTime()] method; 2. Use the [getFullYear()] method; 3. Use the [getMonth()] method; 4. Use the [getDate()] Method; 5. Use the [getHours()] method.

What is the method to get date in javascript

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.

Javascript method to get the date:

1. getTime() method

getTime() method returns the date since 1970 Number of milliseconds since January 1:

Instance

var d = new Date();
document.getElementById("demo").innerHTML = d.getTime();

2. getFullYear() method

getFullYear() method is in the form of four digits Return date and year:

Instance

var d = new Date();
document.getElementById("demo").innerHTML = d.getFullYear();

3. getMonth() method

getMonth() returns the month of the date as a number (0-11) :

Example

var d = new Date();
document.getElementById("demo").innerHTML = d.getMonth();

In JavaScript, the first month (January) is month number 0, so December returns month number 11.

You can use an array of names and use getMonth() to return the month as a name:

Example

var d = new Date();
var months = 
            [
            "January", "February", "March", "April", "May", "June", 
            "July", "August", "September", "October", "November", "December"
            ];
document.getElementById("demo").innerHTML = months[d.getMonth()];

4, getDate() method

getDate() method returns the day of the date as a number (1-31):

Example

var d = new Date();
document.getElementById("demo").innerHTML = d.getDate();

5, getHours() method

getHours() method returns the number of hours of the date as a number (0-23):

Example

var d = new Date();
document.getElementById("demo").innerHTML = d.getHours();

Related free learning recommendations: javascript video tutorial

The above is the detailed content of What is the method to get date in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn