encountered a scene, need to get the specified time range every day, meet the format of "YYYYMMDD", a simple function, the simple idea of

date "to object to a lot of methods, use the following:

new date (date) generating object you can directly specify the date, new date (year, month, day)

getFullYear () returns the date object in

(getMonth) year return date objects in the month (0~11), starting from 0

note (getDate) returns a date object of attention from the day, 1 meter

(getTime) in January 1, 1970 to return to the date object

in accordance with the provisions of the scope of the specified resolution yyyy-mm-dd格式字符串输入时间范围,split分割即可得到开始和结束时间的年月日,再生成对应的date对象,拿到毫秒数

  let st = start.split('-');  let et = end.split('-');  let startTime = new Date(st[0],st[1]-1,st[2]).getTime();  let endTime = new Date(et[0],et[1]-1,et[2]).getTime();

  注意 : 月份需要减 1 ,因为是从0开始计的

拿到每一天

  如何知道时间范围内有那些天呢? The above is the beginning and end of the number of milliseconds time distance 1970.1.1, every day there are 24 * 60 * 60 * 1000 milliseconds, so we can calculate the number of milliseconds per day

 for (let I = startTime; I = < endTime; res.push (formatTime) {(I '')); I + = 24 * 60 * 60 * 1000;}

will format the output time format, a digit 0, plus the specified separator

 function formatTime (time, spliter = {let date = new -) Date (time); let = year date.getFullYear (let); month = (date.getMonth) + 1 (> = 10); (date.getMonth) +? (1):'0'(+) date.getMonth (+ 1); let day = date.getDate (> = 10); date.getDate (?)'0' + date.getDate (return `${y); Ear}${spliter}${month}${spliter}${day}}

</p>
<p><span style= "color: #ff0000" >

summarized above is small to the specified time range list JavaScript generation introduce, I hope to help you, if you have any questions please. I will reply your message, small time. Thank you for your support for the home website of the script!

you might be interested in this article:


This concludes the body part

This paper fixed link:http://www.script-home.com/javascript-generates-a-specified-range-of-time-list.html | Script Home | +Copy Link

Article reprint please specify:JavaScript generates a specified range of time list | Script Home

You may also be interested in these articles!