Home > Web Front-end > JS Tutorial > How to set time in javascript

How to set time in javascript

coldplay.xixi
Release: 2023-01-04 09:35:06
Original
6048 people have browsed it

Javascript method to set time: 1. [setFullYear()] method sets the year of the date object; 2. [setMonth()] method sets the month of the date object; 3. [setDate()] method sets the date The date of the object.

How to set time in javascript

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

Javascript method to set time:

setFullYear() method

setFullYear() method sets the year of the date object. This example is set to 2020:

Instance

<script>
var d = new Date();
d.setFullYear(2020);
document.getElementById("demo").innerHTML = d;
</script>
Copy after login

setFullYear() method can optionally set the month and day:

Instance

<script>
var d = new Date();
d.setFullYear(2020, 11, 3);
document.getElementById("demo").innerHTML = d;
</script>
Copy after login

setMonth () Method

setMonth() method sets the month of the date object (0-11):

Instance

<script>
var d = new Date();
d.setMonth(11);
document.getElementById("demo").innerHTML = d;
</script>
Copy after login

setDate() method

setDate() method sets the day of the date object (1-31):

Example

<script>
var d = new Date();
d.setDate(15);
document.getElementById("demo").innerHTML = d;
</script>
Copy after login

The setDate() method can also be used to add days to a date:

Example

<script>
var d = new Date();
d.setDate(d.getDate() + 50);
document.getElementById("demo").innerHTML = d;
</script>
Copy after login

If you add days, switch months or years, the changes will be handled automatically by the Date object.

setHours() method

setHours() method sets the hour (0-23) of the date object:

Example

<script>
var d = new Date();
d.setHours(22);
document.getElementById("demo").innerHTML = d;
</script>
Copy after login

setMinutes() method

setMinutes() method sets the minutes of the date object (0-59):

Instance

<script>
var d = new Date();
d.setMinutes(30);
document.getElementById("demo").innerHTML = d;
</script>
Copy after login

setSeconds () Method

setSeconds() method sets the seconds of the date object (0-59):

Example

<script>
var d = new Date();
d.setSeconds(30);
document.getElementById("demo").innerHTML = d;
</script>
Copy after login

Compare dates

Dates can be easily compared.

The following example compares today with January 16, 2049:

Example

var today, someday, text;
today = new Date();
someday = new Date();
someday.setFullYear(2049, 0, 16);
if (someday > today) {
  text = "今天在 2049 年 1 月 16 日之前";
} else {
  text = "今天在 2049 年 1 月 16 日之后";
}
document.getElementById("demo").innerHTML = text;
Copy after login

Related free learning recommendations: javascript(Video)

The above is the detailed content of How to set time 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