Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the JavaScript date setting methods?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report