js extracting a specific period of time interval

 

Project encountered a problem and needs to extract the user's selected time interval of a segment, then the background request.

 

The basic idea, according to new Date () obtains the date start_time and end_time objects timestamp, the timestamp is then converted to the new Date () object, and then obtain the corresponding.

 

Here a demo function: date seeking a few days ago, the idea is to find the corresponding time stamp, and then find the date.

 

           getTimeBefore(day) {
                var d = new Date();
                var timestamp = d.getTime() - 1000 * 24 * 60 * 60 * (parseInt(day) - 1);
                var begin = new Date(timestamp);
                var year = begin.getFullYear();
                var month = begin.getMonth() + 1;
                var day = begin.getDate();
                return year + '-' + ('0' + month).substr(-2) + '-' + ('0' + day).substr(-2);
            }

 

Guess you like

Origin www.cnblogs.com/chenmz1995/p/11536617.html