Home >Web Front-end >JS Tutorial >JS generates a time list within a specified range

JS generates a time list within a specified range

php中世界最好的语言
php中世界最好的语言Original
2018-03-20 10:24:481139browse

This time I will bring you JS to generate a time list in a specified range. What are the precautions for JS to generate a time list in a specified range? The following is a practical case, let's take a look.

Encountered a scenario where you need to get every day within the specified time range, satisfying the format "YYYYMMDD", simple function, simple idea

Preparation

 date

Object There are many methods, the following are used:

new date () generates a date object, you can directly specify the year, month, day, etc., new date (year, month, day)

GetFullYear() returns the year in the date object

getMonth() returns the month (0~11) in the date object, note that counting starts from 0

getDate () Returns the day in the date object, note that counting starts from 1

getTime() returns the number of milliseconds from January 1, 1970 to the date object

Parse the specified range

It is specified to follow the yyyy-mm-dd format

StringInput the time range, split to get the year, month and day of the start and end time, and then generate the corresponding date object to get the milliseconds

  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();
Note: The month needs to be subtracted by 1 because it starts from 0.

Get each day

How to know those days in the time range? The above has the number of milliseconds from the start and end time to 1970.1.1, and each day has 24 * 60 * 60 * 1000 milliseconds, so we can calculate each day by the number of milliseconds

    for( let i = startTime ; i <= endTime ; ){
        res.push(formatTime(i,&#39;&#39;));
        i += 24 * 60 * 60 * 1000;
    }

format Output

Format the time, fill in the single digits with 0, and add the specified separator

function formatTime(time,spliter = &#39;-&#39;){
    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 `${year}${spliter}${month}${spliter}${day}`
}
Verification

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Inheritance and prototype chain in JavaScript

How text-align achieves alignment at both ends

The above is the detailed content of JS generates a time list within a specified range. 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