date.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // date function
  2. function date_range_calculator(n, d = new Date(), last = false) {
  3. const firDay = days_left_in_range(n, d)
  4. let lasDay
  5. firDay.setMonth(firDay.getMonth() - (n - 1))
  6. firDay.setDate(1)
  7. if (last) {
  8. firDay.setMonth(firDay.getMonth() - n)
  9. lasDay = days_left_in_range(n, firDay)
  10. } else {
  11. lasDay = days_left_in_range(n, d)
  12. }
  13. return [firDay, lasDay]
  14. }
  15. function date_to_sting(d, type = 1) {
  16. var date = new Date(d);
  17. switch (type) {
  18. case 1:
  19. return moment(date).format('YYYY-MM-DD');
  20. case 2:
  21. return moment(date).format('YYYYMMDD');
  22. default:
  23. break;
  24. }
  25. }
  26. function get_now_time_stamp() {
  27. return Math.round(new Date().getTime() / 1000);
  28. }
  29. function get_time_stamp_for(date) {
  30. return date.getTime() / 1000;
  31. }
  32. function days_left_in_range(n, d) {
  33. d = d || new Date();
  34. var qEnd = new Date(d);
  35. qEnd.setMonth(qEnd.getMonth() + n - qEnd.getMonth() % n, 0);
  36. return qEnd;
  37. }
  38. function last_date(_today) {
  39. let day = new Date(_today.getFullYear(), _today.getMonth()+1,0);
  40. day = _today.getFullYear()+"-"+("0"+(day.getMonth()+1)).slice(-2)+"-"
  41. +("0"+(day.getDate())).slice(-2);
  42. return day;
  43. }
  44. function to_date(date_str) {
  45. var yyyyMMdd = String(date_str);
  46. var sYear = yyyyMMdd.substring(0,4);
  47. var sMonth = yyyyMMdd.substring(4,6);
  48. var sDate = yyyyMMdd.substring(6,8);
  49. return new Date(Number(sYear), Number(sMonth)-1, Number(sDate));
  50. }
  51. function date_range_vending_machine(date_range, current_date = moment(new Date()).format('YYYY-MM-DD'), mode = 0) {
  52. let firDay = '1990-01-01', lasDay = '2100-12-31';
  53. let currDay = ''
  54. switch (date_range) {
  55. case 'day':
  56. currDay = new Date(current_date)
  57. currDay.setDate(currDay.getDate() + mode)
  58. firDay = currDay
  59. lasDay = currDay
  60. firDay.setDate(firDay.getDate())
  61. lasDay.setDate(lasDay.getDate())
  62. break;
  63. case 'yesterday':
  64. firDay = new Date()
  65. lasDay = new Date()
  66. firDay.setDate(firDay.getDate() - 1)
  67. lasDay.setDate(lasDay.getDate() - 1)
  68. break;
  69. case 'week':
  70. currDay = new Date(current_date)
  71. currDay.setDate( currDay.getDate() + (7 * mode) )
  72. firDay = moment(currDay).startOf('isoWeek').format('YYYY-MM-DD')
  73. lasDay = moment(firDay).day(+7)
  74. break;
  75. case 'last-week':
  76. firDay = moment(new Date()).startOf('isoWeek').format('YYYY-MM-DD')
  77. firDay = moment(firDay).day(-6)
  78. lasDay = moment(firDay).day(+7)
  79. break;
  80. case 'month':
  81. currDay = new Date(current_date);
  82. currDay.setMonth( currDay.getMonth() + mode );
  83. [firDay, lasDay] = date_range_calculator(1, currDay)
  84. break;
  85. case 'last-month':
  86. [firDay, lasDay] = date_range_calculator(1, true)
  87. break;
  88. case 'quarterly':
  89. currDay = new Date(current_date);
  90. currDay.setMonth( currDay.getMonth() + (3 * mode) );
  91. [firDay, lasDay] = date_range_calculator(3, currDay)
  92. break;
  93. case 'last-quarterly':
  94. [firDay, lasDay] = date_range_calculator(3, true)
  95. break;
  96. case 'semiannual':
  97. currDay = new Date(current_date);
  98. currDay.setMonth( currDay.getMonth() + (6 * mode) );
  99. [firDay, lasDay] = date_range_calculator(6, currDay)
  100. break;
  101. case 'last-semiannual':
  102. [firDay, lasDay] = date_range_calculator(6, true)
  103. break;
  104. case 'year':
  105. currDay = new Date(current_date);
  106. currDay.setMonth( currDay.getMonth() + (12 * mode) );
  107. [firDay, lasDay] = date_range_calculator(12, currDay)
  108. break;
  109. case 'last-year':
  110. [firDay, lasDay] = date_range_calculator(12, true)
  111. break;
  112. default:
  113. break;
  114. }
  115. return [firDay, lasDay, currDay]
  116. }
  117. function timeForToday(value) {
  118. value.time
  119. const today = new Date();
  120. const timeValue = new Date(Number(value + '000'));
  121. const betweenTime = Math.floor((today.getTime() - timeValue.getTime()) / 1000 / 60);
  122. if (betweenTime < 1) return '방금전';
  123. if (betweenTime < 60) {
  124. return `${betweenTime}분전`;
  125. }
  126. const betweenTimeHour = Math.floor(betweenTime / 60);
  127. if (betweenTimeHour < 24) {
  128. return `${betweenTimeHour}시간전`;
  129. }
  130. const betweenTimeDay = Math.floor(betweenTime / 60 / 24);
  131. if (betweenTimeDay < 365) {
  132. return `${betweenTimeDay}일전`;
  133. }
  134. return `${Math.floor(betweenTimeDay / 365)}년전`;
  135. }