Use js to obtain a set of dates (and count the same day into) consecutive days. The specific content is as follows
First, there must be a set of dates, such as:
var arr = [ '2016/02/28', '2016/02/29', '2017/02/26', '2017/02/27', '2017/02/28' ]; //Date format needs attention,Because considering the continuity of dates to be judged,The elements in the array will be converted into timestamps;
Then to add the current date, get:
var date = new Date(); var y = (); var m = ()+1; var d = (); var today = y+'/'+m+'/'+d;
Turn timestamp method:
//Replace time stampfunction time(date){ return new Date(date); }
Start judging the count:
var num = 0;//Declare count variables;var le = ;//Array length;if(time(today)-time(arr[le-1])==86400000) //Date timestamps are different for one day and are continuous; determine the current date and the most recent day{ num=2;//Satisfies the conditions and last for 2 consecutive days; //Then judge the array loop, if it is satisfied, num++; otherwise, the loop will be interrupted; for(var i=le; i>0; i--){ if(time(arr[i-1])-time(arr[i-2])==86400000){ num++; }else{ break;//If you only need to find all consecutive days, no interruption is required } (num); } }else{ ('The first day'); }
In order to facilitate the effect, the current date can be initialized;
today = '2017/03/01';//To verify the initialization of February to March; it can be omitted
Complete:
function lianxuDay(){ var arr = [ '2016/02/28', '2016/02/29', '2016/02/26', '2017/02/27', '2017/02/28' ]; var date = new Date(); var y = (); var m = ()+1; var d = (); var today = y+'/'+m+'/'+d; today = '2017/03/01';//To verify the initialization of February to March; it can be omitted (today); //Replace time stamp function time(date){ return new Date(date); } var num = 0;//Declare count variables; var le = ;//Array length; //(time(today)-time(arr[le-1])); if(time(today)-time(arr[le-1])==86400000) //Date and time stamps are different for one day and are continuous. Although this method is stupid, it is practical; judge the current date and the most recent day { num=2;//Satisfies the conditions and last for 2 consecutive days; //Then judge the array loop, if it is satisfied, num++; otherwise, the loop will be interrupted; for(var i=le; i>0; i--){ if(time(arr[i-1])-time(arr[i-2])==86400000){ num++; }else{ break;//If you only need to find all consecutive days, no interruption is required } (num); } }else{ ('The first day'); } } lianxuDay();
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.