一個輸入日期,結束日期,計算週數js的函式
阿新 • • 發佈:2019-02-15
function queryWeekbydate(start,end) { var o= { startTime: null, endTime: null, weekCount: 0, currentDate: function (w) { var n = new Date(this.startTime); n.setDate(n.getDate() + parseInt(w) * 7); var d = this.formate(n); return d; }, //增加天數 addDate:function( newstart,days) { var date = new Date(newstart); date.setDate(date.getDate() + parseInt(days)); return this.formate(date); }, //獲取週數 getweekCount: function (e) { var total = (e.getTime() - this.startTime.getTime()) / 1000; var day = parseInt(total / (24 * 60 * 60)); var wc = parseInt(day / 7); return wc; }, //格式化 formate: function (s) { var r = s.getFullYear() + "-" + (parseInt(s.getMonth()+1) < 10 ? ("0" + (s.getMonth()+1)) : (s.getMonth()+1)) + "-" + (parseInt(s.getDate()) < 10 ? ("0" + s.getDate()) : s.getDate()); return r; } } var array = start.toString().split('-'); var array2 = end.toString().split('-'); o.startTime = new Date(array[0], Number(array[1]) - 1, Number(array[2])); //星期幾 var newS = o.startTime.getDay(); var day = newS - 1; o.startTime.setDate(o.startTime.getDate() - day); o.endTime = new Date(array2[0], Number(array2[1]) - 1, Number(array2[2])); o.weekCount = function(end,o) { var c = o.getweekCount(end); return c; }(o.endTime,o); return o; }
今天在公司寫的,真特麼費勁,留著以後用