date.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. (function($) {
  2. $.fn.date = async function(options) {
  3. };
  4. $.fn.date.days_left_in_range = function (n, d) {
  5. d = d || new Date();
  6. var qEnd = new Date(d);
  7. qEnd.setMonth(qEnd.getMonth() + n - qEnd.getMonth() % n, 0);
  8. return qEnd;
  9. }
  10. $.fn.date.range_calculator = function (n, d = new Date(), last = false) {
  11. const firDay = $.fn.date.days_left_in_range(n, d)
  12. let lasDay
  13. firDay.setMonth(firDay.getMonth() - (n - 1))
  14. firDay.setDate(1)
  15. if (last) {
  16. firDay.setMonth(firDay.getMonth() - n)
  17. lasDay = $.fn.date.days_left_in_range(n, firDay)
  18. } else {
  19. lasDay = $.fn.date.days_left_in_range(n, d)
  20. }
  21. return [firDay, lasDay]
  22. }
  23. $.fn.date.rangeVendingMachine = function (date_range, current_date = moment(new Date()).format('YYYY-MM-DD'), mode = 0) {
  24. let firDay = '1990-01-01', lasDay = '3000-12-31';
  25. let currDay = ''
  26. switch (date_range) {
  27. case 'day':
  28. currDay = new Date(current_date)
  29. currDay.setDate(currDay.getDate() + mode)
  30. firDay = currDay
  31. lasDay = currDay
  32. firDay.setDate(firDay.getDate())
  33. lasDay.setDate(lasDay.getDate())
  34. break;
  35. case 'week':
  36. currDay = new Date(current_date)
  37. currDay.setDate( currDay.getDate() + (7 * mode) )
  38. firDay = moment(currDay).startOf('isoWeek').format('YYYY-MM-DD')
  39. lasDay = moment(firDay).day(+7)
  40. break;
  41. case 'month':
  42. currDay = new Date(current_date);
  43. currDay.setMonth( currDay.getMonth() + mode );
  44. [firDay, lasDay] = $.fn.date.range_calculator(1, currDay)
  45. break;
  46. case 'quarterly':
  47. currDay = new Date(current_date);
  48. currDay.setMonth( currDay.getMonth() + (3 * mode) );
  49. [firDay, lasDay] = $.fn.date.range_calculator(3, currDay)
  50. break;
  51. case 'semiannual':
  52. currDay = new Date(current_date);
  53. currDay.setMonth( currDay.getMonth() + (6 * mode) );
  54. [firDay, lasDay] = $.fn.date.range_calculator(6, currDay)
  55. break;
  56. case 'year':
  57. currDay = new Date(current_date);
  58. currDay.setMonth( currDay.getMonth() + (12 * mode) );
  59. [firDay, lasDay] = $.fn.date.range_calculator(12, currDay)
  60. break;
  61. default:
  62. break;
  63. }
  64. return [firDay, lasDay, currDay]
  65. }
  66. }(jQuery));