dataFormate.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**
  2. * 获取本周、本季度、本月、上月的开始日期、结束日期
  3. */
  4. var now = new Date(); //当前日期
  5. var nowDayOfWeek = now.getDay(); //今天本周的第几天
  6. var nowDay = now.getDate(); //当前日
  7. var nowMonth = now.getMonth(); //当前月
  8. var nowYear = now.getYear(); //当前年
  9. nowYear += (nowYear < 2000) ? 1900 : 0; //
  10. var lastMonthDate = new Date(); //上月日期
  11. lastMonthDate.setDate(1);
  12. lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
  13. // var lastYear = lastMonthDate.getYear();
  14. var lastMonth = lastMonthDate.getMonth();
  15. //格式化日期:yyyy-MM-dd
  16. function formatDate(date) {
  17. var myyear = date.getFullYear();
  18. var mymonth = date.getMonth() + 1;
  19. var myweekday = date.getDate();
  20. if (mymonth < 10) {
  21. mymonth = "0" + mymonth;
  22. }
  23. if (myweekday < 10) {
  24. myweekday = "0" + myweekday;
  25. }
  26. return (myyear + "-" + mymonth + "-" + myweekday);
  27. }
  28. //获得某月的天数
  29. function getMonthDays(myMonth) {
  30. var monthStartDate = new Date(nowYear, myMonth, 1);
  31. var monthEndDate = new Date(nowYear, myMonth + 1, 1);
  32. var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
  33. return days;
  34. }
  35. //获得本季度的开始月份
  36. function getQuarterStartMonth() {
  37. var quarterStartMonth = 0;
  38. if (nowMonth < 3) {
  39. quarterStartMonth = 0;
  40. }
  41. if (2 < nowMonth && nowMonth < 6) {
  42. quarterStartMonth = 3;
  43. }
  44. if (5 < nowMonth && nowMonth < 9) {
  45. quarterStartMonth = 6;
  46. }
  47. if (nowMonth > 8) {
  48. quarterStartMonth = 9;
  49. }
  50. return quarterStartMonth;
  51. }
  52. //获得本周的开始日期
  53. function getWeekStartDate() {
  54. var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1);
  55. return formatDate(weekStartDate) + ' 00:00:00';
  56. }
  57. //获得本周的结束日期
  58. function getWeekEndDate() {
  59. var weekEndDate = new Date(nowYear, nowMonth, nowDay + (7 - nowDayOfWeek));
  60. return formatDate(weekEndDate) + ' 23:59:59';
  61. }
  62. //获得上周的开始日期
  63. function getLastWeekStartDate() {
  64. var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 7);
  65. return formatDate(weekStartDate) + ' 00:00:00';
  66. }
  67. //获得上周的结束日期
  68. function getLastWeekEndDate() {
  69. var weekEndDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 1);
  70. return formatDate(weekEndDate) + ' 23:59:59';
  71. }
  72. //获得本月的开始日期
  73. function getMonthStartDate() {
  74. var monthStartDate = new Date(nowYear, nowMonth, 1);
  75. return formatDate(monthStartDate) + ' 00:00:00';
  76. }
  77. //获得本月的结束日期
  78. function getMonthEndDate() {
  79. var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
  80. return formatDate(monthEndDate) + ' 23:59:59';
  81. }
  82. //获得上月开始时间
  83. function getLastMonthStartDate() {
  84. var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
  85. return formatDate(lastMonthStartDate) + ' 00:00:00';
  86. }
  87. //获得上月结束时间
  88. function getLastMonthEndDate() {
  89. var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
  90. return formatDate(lastMonthEndDate) + ' 23:59:59';
  91. }
  92. //获得本季度的开始日期
  93. function getQuarterStartDate() {
  94. var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
  95. return formatDate(quarterStartDate) + ' 00:00:00';
  96. }
  97. //或的本季度的结束日期
  98. function getQuarterEndDate() {
  99. var quarterEndMonth = getQuarterStartMonth() + 2;
  100. var quarterStartDate = new Date(nowYear, quarterEndMonth,
  101. getMonthDays(quarterEndMonth));
  102. return formatDate(quarterStartDate) + ' 23:59:59';
  103. }
  104. //获得前半年开始时间
  105. function getHalfYearStartDate() {
  106. var firstHalfYearStartDate = new Date(nowYear, 1 - 1, 1);
  107. return formatDate(firstHalfYearStartDate) + ' 00:00:00';
  108. }
  109. //获前半年结束时间
  110. function getHalfYearEndDate() {
  111. var firstHalfYearEndDate = new Date(nowYear, 6 - 1, 30);
  112. return formatDate(firstHalfYearEndDate) + ' 23:59:59';
  113. }
  114. //获得前今年开始时间
  115. function getThisYearStartDate() {
  116. var firstThisYearStartDate = new Date(nowYear, 1 - 1, 1);
  117. return formatDate(firstThisYearStartDate) + ' 00:00:00';
  118. }
  119. //获前今年结束时间
  120. function getThisYearEndDate() {
  121. var firstThisYearEndDate = new Date(nowYear, 12 - 1, 31);
  122. return formatDate(firstThisYearEndDate) + ' 23:59:59';
  123. }
  124. //获当前起前一个月时间
  125. function getThisDateBeforMonth() {
  126. var lastMonthToday = new Date(
  127. new Date().getTime() - 30 * 24 * 60 * 60 * 1000
  128. );
  129. var lastMonthYear = lastMonthToday.getFullYear();
  130. let month = lastMonthToday.getMonth() + 1
  131. var lastMonth = month < 10 ? '0' + month : month;
  132. var lastMonthDay =
  133. lastMonthToday.getDate() < 10 ?
  134. '0' + lastMonthToday.getDate() :
  135. lastMonthToday.getDate();
  136. var lastMonthKsrq = lastMonthYear + "-" + lastMonth + "-" + lastMonthDay + " 00:00:00";
  137. return lastMonthKsrq
  138. }
  139. //获当前起后一个月时间
  140. function getThisDateNextMonth() {
  141. var nextMonthToday = new Date(
  142. new Date().getTime() + 30 * 24 * 60 * 60 * 1000
  143. );
  144. var nextMonthYear = nextMonthToday.getFullYear();
  145. let month = nextMonthToday.getMonth() + 1
  146. var nextMonth = month < 10 ? '0' + month : month;
  147. var nextMonthDay =
  148. nextMonthToday.getDate < 10 ?
  149. "0" + nextMonthToday.getDate :
  150. nextMonthToday.getDate();
  151. var nextMonthJsrq = nextMonthYear + "-" + nextMonth + "-" + nextMonthDay;
  152. return nextMonthJsrq
  153. }
  154. // 中国标准时间转年月日时分秒
  155. function timestampToTime(timestamp) {
  156. // var chinaStandard=Mon Jul 19 2021 11:11:55 GMT+0800 (中国标准时间);
  157. var date = new Date(timestamp);
  158. var y = date.getFullYear();
  159. var m = date.getMonth() + 1;
  160. m = m < 10 ? ('0' + m) : m;
  161. var d = date.getDate();
  162. d = d < 10 ? ('0' + d) : d;
  163. var h = date.getHours();
  164. h = h < 10 ? ('0' + h) : h;
  165. var minute = date.getMinutes();
  166. minute = minute < 10 ? ('0' + minute) : minute;
  167. var second = date.getSeconds();
  168. second = second < 10 ? ('0' + second) : second;
  169. let time = y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
  170. return time
  171. }
  172. //获取当前日期yy-mm-dd
  173. //date 为时间对象
  174. function getDateStr3(date) {
  175. var year = "";
  176. var month = "";
  177. var day = "";
  178. var now = date;
  179. year = "" + now.getFullYear();
  180. if ((now.getMonth() + 1) < 10) {
  181. month = "0" + (now.getMonth() + 1);
  182. } else {
  183. month = "" + (now.getMonth() + 1);
  184. }
  185. if ((now.getDate()) < 10) {
  186. day = "0" + (now.getDate());
  187. } else {
  188. day = "" + (now.getDate());
  189. }
  190. return year + "-" + month + "-" + day;
  191. }
  192. /**
  193. * 获得相对当前周AddWeekCount个周的起止日期
  194. * AddWeekCount为0代表当前周 为-1代表上一个周 为1代表下一个周以此类推
  195. * **/
  196. function getWeekStartAndEnd(AddWeekCount) {
  197. //起止日期数组
  198. var startStop = new Array();
  199. //一天的毫秒数
  200. var millisecond = 1000 * 60 * 60 * 24;
  201. //获取当前时间
  202. var currentDate = new Date();
  203. //相对于当前日期AddWeekCount个周的日期
  204. currentDate = new Date(currentDate.getTime() + (millisecond * 7 * AddWeekCount));
  205. //返回date是一周中的某一天
  206. var week = currentDate.getDay();
  207. //返回date是一个月中的某一天
  208. // var month = currentDate.getDate();
  209. //减去的天数
  210. var minusDay = week != 0 ? week - 1 : 6;
  211. //获得当前周的第一天
  212. var currentWeekFirstDay = new Date(currentDate.getTime() - (millisecond * minusDay));
  213. //获得当前周的最后一天
  214. var currentWeekLastDay = new Date(currentWeekFirstDay.getTime() + (millisecond * 6));
  215. //添加至数组
  216. startStop.push(getDateStr3(currentWeekFirstDay));
  217. startStop.push(getDateStr3(currentWeekLastDay));
  218. return startStop;
  219. }
  220. /**
  221. * 获得相对当月AddMonthCount个月的起止日期
  222. * AddMonthCount为0 代表当月 为-1代表上一个月 为1代表下一个月 以此类推
  223. * ***/
  224. function getMonthStartAndEnd(AddMonthCount) {
  225. //起止日期数组
  226. var startStop = new Array();
  227. //获取当前时间
  228. var currentDate = new Date();
  229. var month = currentDate.getMonth() + AddMonthCount;
  230. if (month < 0) {
  231. var n = parseInt((-month) / 12);
  232. month += n * 12;
  233. currentDate.setFullYear(currentDate.getFullYear() - n);
  234. }
  235. currentDate = new Date(currentDate.setMonth(month));
  236. //获得当前月份0-11
  237. var currentMonth = currentDate.getMonth();
  238. //获得当前年份4位年
  239. var currentYear = currentDate.getFullYear();
  240. //获得上一个月的第一天
  241. var currentMonthFirstDay = new Date(currentYear, currentMonth, 1);
  242. //获得上一月的最后一天
  243. var currentMonthLastDay = new Date(currentYear, currentMonth + 1, 0);
  244. //添加至数组
  245. startStop.push(getDateStr3(currentMonthFirstDay));
  246. startStop.push(getDateStr3(currentMonthLastDay));
  247. //返回
  248. return startStop;
  249. }
  250. /**
  251. * 获得当前年月日
  252. * ***/
  253. function YearMonthDate() {
  254. var date = new Date();
  255. var mon = date.getMonth() + 1;
  256. var day = date.getDate();
  257. var currDate = date.getFullYear() + "-" + (mon < 10 ? "0" + mon : mon) + "-" + (day < 10 ? "0" + day : day) + " 23:59:59";
  258. return currDate
  259. }
  260. /**
  261. * 获得当前年月日SFM
  262. * ***/
  263. function YearMonthDateSFN() {
  264. var date = new Date();
  265. var mon = date.getMonth() + 1;
  266. var day = date.getDate();
  267. var h = date.getHours();
  268. h = h < 10 ? ('0' + h) : h;
  269. var minute = date.getMinutes();
  270. minute = minute < 10 ? ('0' + minute) : minute;
  271. var second = date.getSeconds();
  272. second = second < 10 ? ('0' + second) : second;
  273. var currDate = date.getFullYear() + "-" + (mon < 10 ? "0" + mon : mon) + "-" + (day < 10 ? "0" + day : day) + " " + h + ":" + minute + ":" + second;
  274. return currDate
  275. }
  276. /**
  277. * 年月日时分秒转时间戳()
  278. * getTimeFormat('2022-02-02 02:22:22');
  279. * ***/
  280. function getTimeFormat(timeS){
  281. let time = (new Date(timeS).getTime()) / 1000; //除1000 是变成秒级的时间戳 不除就是毫秒级
  282. return time;
  283. }
  284. export {
  285. getWeekStartAndEnd,
  286. getMonthStartAndEnd,
  287. getHalfYearStartDate,
  288. getHalfYearEndDate,
  289. getThisYearStartDate,
  290. getThisYearEndDate,
  291. getWeekStartDate,
  292. getWeekEndDate,
  293. getLastWeekStartDate,
  294. getLastWeekEndDate,
  295. getMonthStartDate,
  296. getMonthEndDate,
  297. getLastMonthStartDate,
  298. getLastMonthEndDate,
  299. getQuarterStartDate,
  300. getQuarterEndDate,
  301. timestampToTime,
  302. getThisDateBeforMonth,
  303. getThisDateNextMonth,
  304. YearMonthDate,
  305. YearMonthDateSFN,
  306. getTimeFormat
  307. }