Home > Web Front-end > JS Tutorial > body text

What is the method to get date in javascript

coldplay.xixi
Release: 2023-01-05 16:08:35
Original
5011 people have browsed it

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();
Copy after login

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();
Copy after login

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();
Copy after login

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()];
Copy after login

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();
Copy after login

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();
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!