File indexing completed on 2025-02-02 05:10:28

0001 
0002 
0003 /**
0004 * Returns the week number for this date.  dowOffset is the day of week the week
0005 * "starts" on for your locale - it can be from 0 to 6. If dowOffset is 1 (Monday),
0006 * the week returned is the ISO 8601 week number.
0007 * @param int dowOffset
0008 * @return int
0009 */
0010 function getWeek(date, dowOffset) {
0011     var newYear = new Date(date.getFullYear(),0,1);
0012     var day = newYear.getDay() - dowOffset; //the day of week the year begins on
0013     day = (day >= 0 ? day : day + 7);
0014     var daynum = Math.floor((date.getTime() - newYear.getTime() - (date.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
0015     var weeknum;
0016     //if the year starts before the middle of a week
0017     if(day < 4) {
0018         weeknum = Math.floor((daynum+day-1)/7) + 1;
0019         if(weeknum > 52) {
0020             var nYear = new Date(date.getFullYear() + 1,0,1);
0021             var nday = nYear.getDay() - dowOffset;
0022             nday = nday >= 0 ? nday : nday + 7;
0023             /*if the next year starts before the middle of
0024             the week, it is week #1 of that year*/
0025             weeknum = nday < 4 ? 1 : 53;
0026         }
0027     }
0028     else {
0029         weeknum = Math.floor((daynum+day-1)/7);
0030     }
0031     return weeknum;
0032 }
0033 
0034 function roundToDay(date) {
0035     return new Date(date.getFullYear(), date.getMonth(), date.getDate())
0036 }
0037 
0038 function roundToMinutes(date, delta) {
0039     var totalMinutes = date.getHours() * 60 +  date.getMinutes()
0040     //Round to nearest delta
0041     totalMinutes = Math.round(totalMinutes / delta) * delta
0042     var minutes = totalMinutes % 60
0043     var hours = (totalMinutes - minutes) / 60
0044     return new Date(date.getFullYear(), date.getMonth(), date.getDate(), hours, minutes, 0)
0045 }
0046 
0047 function addDaysToDate(date, days) {
0048     var date = new Date(date);
0049     date.setDate(date.getDate() + days);
0050     return date;
0051 }
0052 
0053 function addMinutesToDate(date, minutes) {
0054     return new Date(date.getTime() + minutes*60000);
0055 }
0056 
0057 function sameDay(date1, date2) {
0058     return date1.getFullYear() == date2.getFullYear() && date1.getMonth() == date2.getMonth() && date1.getDate() == date2.getDate()
0059 }
0060 
0061 function sameMonth(date1, date2) {
0062     return date1.getFullYear() == date2.getFullYear() && date1.getMonth() == date2.getMonth()
0063 }
0064 
0065 function nextWeek(date) {
0066     var d = date
0067     d.setTime(date.getTime() + (24*60*60*1000) * 7);
0068     return d
0069 }
0070 
0071 function previousWeek(date) {
0072     var d = date
0073     d.setTime(date.getTime() - (24*60*60*1000) * 7);
0074     return d
0075 }
0076 
0077 function nextMonth(date) {
0078     var d = date
0079     //FIXME, if you add a month to the 31.3 you will end up on the 5.1 because April has only 30 days. Wtf javascript
0080     d.setMonth(date.getMonth() + 1);
0081     return d
0082 }
0083 
0084 function previousMonth(date) {
0085     var d = date
0086     d.setMonth(date.getMonth() - 1);
0087     return d
0088 }
0089 
0090 function getFirstDayOfWeek(date) {
0091     //This works with negative days to get to the previous month
0092     //Date.getDate() is the day of the month, Date.getDay() is the day of the week
0093     // Examples:
0094     // 28 = 28 - (1 - 1)
0095     // 21 = 27 - (0 - 1)
0096     // 21 = 26 - (6 - 1)
0097     // 21 = 25 - (5 - 1)
0098     var offset = date.getDay() - Qt.locale().firstDayOfWeek;
0099     if (offset < 0) {
0100         offset = 7 + offset;
0101     }
0102     return new Date(date.getFullYear(), date.getMonth(), date.getDate() - offset)
0103 }
0104 
0105 function getFirstDayOfMonth(date) {
0106     var d = date
0107     d.setDate(1)
0108     return d
0109 }
0110 
0111 function daysSince(date1, date2) {
0112     var d1 = roundToDay(date1)
0113     var d2 = roundToDay(date2)
0114     const oneDay = 24 * 60 * 60 * 1000;
0115     return Math.round(Math.abs((d1.getTime() - d2.getTime()) / oneDay));
0116 }
0117