util.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //获取年
  2. export const getYears = (start,end)=>{
  3. const years = [];
  4. while(start <= end){
  5. years.push(start);
  6. ++start;
  7. }
  8. return years;
  9. }
  10. //获取月
  11. export const getMonths = ()=>{
  12. let index = 1;
  13. const months = [];
  14. while(index <= 12){
  15. if(index < 10){
  16. months.push('0'+index);
  17. }else{
  18. months.push(index);
  19. }
  20. ++index;
  21. }
  22. return months;
  23. }
  24. //获取日
  25. export const getDays = (year,month)=>{
  26. const days = [];
  27. //获取当月有几天
  28. const count = new Date(year,month,0).getDate();
  29. let index = 1;
  30. while(index <= count){
  31. if(index < 10){
  32. days.push('0'+index);
  33. }else{
  34. days.push(index);
  35. }
  36. ++index;
  37. }
  38. return days;
  39. }
  40. //获取小时
  41. export const getHours = ()=>{
  42. return createArray(24);
  43. }
  44. //获取分钟
  45. export const getMinutes = ()=>{
  46. return createArray(60);
  47. }
  48. //获取秒
  49. export const getSeconds = ()=>{
  50. return createArray(60);
  51. }
  52. //根据日期获取年月日时分秒
  53. export const getDateTime = (date)=>{
  54. const year = date.getFullYear();
  55. const month = date.getMonth()+1;
  56. const day = date.getDate();
  57. const hour = date.getHours();
  58. const minute = date.getMinutes();
  59. const second = date.getSeconds();
  60. return [String(year),String(addZero(month)),String(addZero(day)),String(addZero(hour)),String(addZero(minute)),String(addZero(second))];
  61. }
  62. export const getDateStringTime = (dateString)=>{
  63. return dateString.split('-').map(item=>addZero(item)).join('-')
  64. }
  65. //创建确定长度的数组
  66. export const createArray = count =>{
  67. return Array.from({length: count},(value,index)=>addZero(index))
  68. }
  69. //<10的数前面加0
  70. export const addZero = (num)=>{
  71. return num < 10 ? '0'+num : num;
  72. }
  73. /**
  74. * YYYY 2020
  75. * MM 01-12
  76. * DD 01-31
  77. * HH 01-24
  78. * mm 00-59
  79. * ss 00-59
  80. * 根据传入的时间数组和类型转化为不同格式的时间字符串
  81. */
  82. export const formateTime = (array, type)=>{
  83. const types = [
  84. "YYYY",
  85. "YYYY-MM",
  86. "YYYY-MM-DD",
  87. "YYYY-MM-DD HH:mm",
  88. "YYYY-MM-DD HH:mm:ss",
  89. ];
  90. const index = types.findIndex((item) => item === type);
  91. if (index > -1) {
  92. const len = index;
  93. if (len > 2) {
  94. array.length = len + 2;
  95. return `${array.slice(0, 3).join("-")} ${array.slice(3).join(":")}`;
  96. } else {
  97. array.length = len + 1;
  98. return `${array.slice(0, 3).join("-")}`;
  99. }
  100. }
  101. return "";
  102. }
  103. /**
  104. * 古时十二时辰
  105. */
  106. export const chinaHours = [
  107. {
  108. name: ' 子时',
  109. startTime: '23:00:00',
  110. endTime: '00:59:59',
  111. },
  112. {
  113. name: '丑时',
  114. startTime: '01:00:00',
  115. endTime: '02:59:59',
  116. },
  117. {
  118. name: '寅时',
  119. startTime: '03:00:00',
  120. endTime: '04:59:59',
  121. },
  122. {
  123. name: '卯时',
  124. startTime: '05:00:00',
  125. endTime: '06:59:59',
  126. },
  127. {
  128. name: '辰时',
  129. startTime: '07:00:00',
  130. endTime: '08:59:59',
  131. },
  132. {
  133. name: '巳时',
  134. startTime: '09:00:00',
  135. endTime: '10:59:59',
  136. },
  137. {
  138. name: '午时',
  139. startTime: '11:00:00',
  140. endTime: '12:59:59',
  141. },
  142. {
  143. name: '未时',
  144. startTime: '13:00:00',
  145. endTime: '14:59',
  146. },
  147. {
  148. name: '申时',
  149. startTime: '15:00:00',
  150. endTime: '16:59:59',
  151. },
  152. {
  153. name: '酉时',
  154. startTime: '17:00:00',
  155. endTime: '18:59:59',
  156. },
  157. {
  158. name: ' 戌时',
  159. startTime: '19:00:00',
  160. endTime: '20:59:59',
  161. },
  162. {
  163. name: '亥时',
  164. startTime: '21:00:00',
  165. endTime: '22:59:59',
  166. },
  167. ]
  168. /**
  169. * time: 传入时间
  170. * 返回:时辰
  171. */
  172. export const getChinaTime = (time)=>{
  173. const currentSeconds = timeToSeconds(time);
  174. const index = chinaHours.findIndex((item,index)=>{
  175. const startSeconds = timeToSeconds(item.startTime);
  176. const endSeconds = timeToSeconds(item.endTime);
  177. if(index == 0){
  178. const startEndSeconds = timeToSeconds('23:59:59');
  179. const endStartSeconds = timeToSeconds('00:00:00');
  180. console.log(index,startSeconds,currentSeconds,startEndSeconds)
  181. console.log(index,endStartSeconds,currentSeconds,endSeconds)
  182. return (currentSeconds >= startSeconds && currentSeconds <= startEndSeconds) || (currentSeconds >= endStartSeconds && currentSeconds <= endSeconds) ;
  183. }else{
  184. return currentSeconds >= startSeconds && currentSeconds <= endSeconds;
  185. }
  186. })
  187. return chinaHours[index];
  188. }
  189. //将时间转化为秒数
  190. export const timeToSeconds = (time)=>{
  191. const timeArray = time.split(':');
  192. const currentTimeSeconds = timeArray.reduce((accumulator,currentValue,index)=>{
  193. if(index == timeArray.length - 1){
  194. return accumulator + Number(currentValue);
  195. }else if(index == 0){
  196. return accumulator + Number(currentValue) * 3600;
  197. }
  198. return accumulator + Number(currentValue) * 60
  199. },0)
  200. return currentTimeSeconds;
  201. }