Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the JavaScript date setting methods?

Shulou Source: shulou.com Published: 2022-06-01 06:50:45 09月28日 Update

This article mainly introduces what the JavaScript date setting method has, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

JavaScript date setting method, full date operation

The setting method is used to set a portion of the date. Here are the most commonly used methods (sorted alphabetically):

Method description

SetDate () sets the day with a numeric value (1-31)

SetFullYear () setting year (optional month and day)

SetHours () setting hours (0-23)

SetMilliseconds () setting milliseconds (0-999)

SetMinutes () setting score (0-59)

SetMonth () setting month (0-11)

SetSeconds () setting seconds (0-59)

SetTime () setting time (milliseconds since January 1, 1970)

SetFullYear () method

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

Example

Var d = new Date ()

D.setFullYear (2020)

Document.getElementById ("demo"). InnerHTML = d

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

Example

Var d = new Date ()

D.setFullYear (2020, 11, 3)

Document.getElementById ("demo"). InnerHTML = d

SetMonth () method

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

Example

Var d = new Date ()

D.setMonth (11)

Document.getElementById ("demo"). InnerHTML = d

SetDate () method

The setDate () method sets the date object's date (1-31):

Example

Var d = new Date ()

D.setDate (15)

Document.getElementById ("demo"). InnerHTML = d

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

Example

Var d = new Date ()

D.setDate (d.getDate () + 50)

Document.getElementById ("demo"). InnerHTML = d

If you add days and switch months or years, the change is handled automatically by the Date object.

SetHours () method

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

Example

Var d = new Date ()

D.setHours (22)

Document.getElementById ("demo"). InnerHTML = d

SetMinutes () method

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

Example

Var d = new Date ()

D.setMinutes (30)

Document.getElementById ("demo"). InnerHTML = d

SetSeconds () method

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

Example

Var d = new Date ()

D.setSeconds (30)

Document.getElementById ("demo"). InnerHTML = d

Compare date

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 = "Today before January 16, 2049"

} else {

Text = "Today is after January 16, 2049"

}

Document.getElementById ("demo"). InnerHTML = text

JavaScript counts months from 0 to 11. January is 0. December is November.

Thank you for reading this article carefully. I hope the article "what are the JavaScript date setting methods" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Methods dates instances objects articles months examples days hours years value interests at the same time letters commonly used numerical values time more friends knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker vpn Xiaomi MySQL Shulou Technology