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

How to set time in javascript

coldplay.xixi
coldplay.xixiOriginal
2021-04-08 11:38:336031browse

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>

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>

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>

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>

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>

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>

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>

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>

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;

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!

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