A small detail about the time span difference in JavaScript processing

Source: Internet
Author: User

The problem is that a time span must be assigned during page initialization. the first time is the first 30 days of the second time, or three days, or another time span. here is the problem of time span.

 

I found a problem yesterday, that is, there were too few considerations in terms of processing time, and I did not fully consider the problem. Why? For other reasons, they all have their own problems, but I found it.

 

Some problems may occur when dealing with cross-year and cross-month data. Of course, there may be better solutions and more comprehensive and more compact usage. However, this stupid method was used at the time, therefore, it is the improvement on this basis.

 

In fact, it is quite simple to use ordinary thinking, that is, the current time, should be well handled, followed by the previous time, directly deal with it.

 

See the followingProgramWe can see that we use the specification time to process the time before the current time as a time format, and then it is the same as the current time, just take the value directly,

 

the procedure is as follows:

Function reloadtime () {<br/> var date = new date (); <br/> var date3 = date. gettime ()-(3*3600*24*1000); // here is the processing time, which is processed according to the standard time, followed by values. you can obtain it directly <br/> var fordate = new date (date3); </P> <p> var hour = date. totimestring (). substring (0, 8); <br/> var month; <br/> var endday; <br/> var stday; <br/> var startmonth; </P> <p> If (date. getmonth () + 1) <10) {<br/> month = '0' + (date. getmonth () + 1) <br/>} else {<br/> month = (date. getmonth () + 1) <br/>}</P> <p> If (date. getdate () <10) {<br/> endday = '0' + date. getdate (); <br/>}else {<br/> endday = date. getdate (); <br/>}</P> <p> // calculate the initial day <br/> If (fordate. getdate () <10) {<br/> stday = '0' + fordate. getdate (); <br/>}else {<br/> stday = fordate. getdate (); <br/>}< br/> // calculate the initial month <br/> If (fordate. getmonth () + 1) <10) {<br/> startmonth = '0' + (fordate. getmonth () + 1) <br/>} else {<br/> startmonth = (fordate. getmonth () + 1) <br/>}< br/> // processing year <br/> // three days ago <br/> var sttime = fordate. getyear () + '-' + startmonth + '-' + stday + ''+ hour; <br/> query. starttime. value = sttime; <br/> // current time <br/> var thistime = date. getyear () + '-' + month + '-' + endday + ''+ hour; <br/> query. endtime. value = thistime; <br/>}</P> <p> for the time, the modification is correct. If you use other methods, it is hard to grasp them. so, ah.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.