File indexing completed on 2025-03-09 04:52:15

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