date.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // date function
  2. function date_range_calculator(n) {
  3. firDay = days_left_in_range(n);
  4. firDay.setMonth(firDay.getMonth() - (n - 1));
  5. firDay.setDate(1);
  6. lasDay = days_left_in_range(n);
  7. return [firDay, lasDay]
  8. }
  9. function date_to_sting(d, type = 1) {
  10. var date = new Date(d);
  11. switch (type) {
  12. case 1:
  13. return moment(date).format('YYYY-MM-DD');
  14. case 2:
  15. return moment(date).format('YYYYMMDD');
  16. default:
  17. break;
  18. }
  19. }
  20. function get_now_time_stamp() {
  21. return Math.round(new Date().getTime() / 1000);
  22. }
  23. function days_left_in_range(n, d) {
  24. d = d || new Date();
  25. var qEnd = new Date(d);
  26. qEnd.setMonth(qEnd.getMonth() + n - qEnd.getMonth() % n, 0);
  27. return qEnd;
  28. }
  29. function last_date(_today) {
  30. day = new Date(_today.getFullYear(), _today.getMonth()+1,0);
  31. day = _today.getFullYear()+"-"+("0"+(day.getMonth()+1)).slice(-2)+"-"
  32. +("0"+(day.getDate())).slice(-2);
  33. return day;
  34. }
  35. function to_date(date_str) {
  36. var yyyyMMdd = String(date_str);
  37. var sYear = yyyyMMdd.substring(0,4);
  38. var sMonth = yyyyMMdd.substring(4,6);
  39. var sDate = yyyyMMdd.substring(6,8);
  40. return new Date(Number(sYear), Number(sMonth)-1, Number(sDate));
  41. }
  42. function date_range_vending_machine(date_range) {
  43. let firDay = '1990-01-01', lasDay = '3000-12-31';
  44. switch (date_range) {
  45. case 'day':
  46. firDay = new Date();
  47. lasDay = new Date();
  48. break;
  49. case 'week':
  50. firDay = moment(new Date()).startOf('isoWeek').format('YYYY-MM-DD');
  51. lasDay = moment(firDay).day(+7);
  52. break;
  53. case 'month':
  54. firDay = new Date();
  55. firDay.setDate(1);
  56. lasDay = last_date(new Date());
  57. break;
  58. case 'quarterly':
  59. [firDay, lasDay] = date_range_calculator(3)
  60. break;
  61. case 'semiannual':
  62. [firDay, lasDay] = date_range_calculator(6)
  63. break;
  64. case 'year':
  65. [firDay, lasDay] = date_range_calculator(12)
  66. break;
  67. default:
  68. break;
  69. }
  70. return [firDay, lasDay];
  71. }