config.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { Lunar } from './lunar'
  2. const LunarMinDate = (new Date(1901, 0, 1)).getTime()
  3. const LunarMaxDate = (new Date(2100, 0, 1)).getTime()
  4. const Weeks = ['日', '一', '二', '三', '四', '五', '六']
  5. const Months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  6. const EnOrders = ['', 'st', 'nd', 'rd', 'th']
  7. const initCurrInfo = { i: 0, x: 0, y: 0, t: false, d: 0, a: false, v: false, s: false }
  8. const ConsoleStyle = {
  9. info: {
  10. label: 'color: #409EFF; font-weight:bold',
  11. content: 'color: #8cc5ff',
  12. title: '提示'
  13. },
  14. warn: {
  15. label: 'color: #f37b1d; font-weight:bold',
  16. content: 'color: #fcdabd',
  17. title: '警告'
  18. }
  19. }
  20. const Today = () => {
  21. const _today = new Date;
  22. return DayDetail(_today.getFullYear(), _today.getMonth() + 1, _today.getDate())
  23. }
  24. const isOverDate = date => {
  25. const queryTime = date.getTime()
  26. return queryTime < LunarMinDate || queryTime >= LunarMaxDate
  27. }
  28. const LunarDetail = (gregorianYear, gregorianMonth, gregorianDay) => {
  29. Lunar.resetInitGregorian(gregorianYear, gregorianMonth, gregorianDay)
  30. Lunar.setGregorian(gregorianYear, gregorianMonth, gregorianDay)
  31. return Lunar.getLunarDate()
  32. }
  33. const DayDetail = (year, month, day, type = '') => {
  34. const date = new Date(year, month - 1, day)
  35. const _year = date.getFullYear()
  36. const _month = date.getMonth() + 1
  37. const _day = date.getDate()
  38. const week = date.getDay()
  39. const week_name = `周${ Weeks[week] }`
  40. const { lunar_order, lunar_year, lunar_month, lunar_day, lunar_date, lunar_type } = (isOverDate(date) ? emptyLunar() : LunarDetail(_year, _month, _day))
  41. return { year: _year, month: _month, day: _day, week, week_name, lunar_order, lunar_year, lunar_month, lunar_day, lunar_type, lunar_date, type }
  42. }
  43. const emptyLunar = () => {
  44. return { lunar_order: '', lunar_year: '', lunar_month: '', lunar_day: '', lunar_date: '', lunar_type: '' }
  45. }
  46. const MonthDaysCount = (year, month) => {
  47. return (new Date(year, month, 0)).getDate()
  48. }
  49. const DayShort = (year, month, day) => {
  50. const date = new Date(year, month - 1, day)
  51. const _year = date.getFullYear()
  52. const _month = date.getMonth() + 1
  53. const _day = date.getDate()
  54. const { lunar_month, lm, ld } = LunarDetail(_year, _month, _day)
  55. return { year: _year, month: _month, day: _day, lunar_month, lm, ld }
  56. }
  57. const MonthOnly = (year, month) => {
  58. const _count = MonthDaysCount(year, month)
  59. return Array.apply(null, { length: _count }).map((_, _i) => DayShort(year, month, _i + 1))
  60. }
  61. const MonthDaysDetail = (year, month) => {
  62. const _count = MonthDaysCount(year, month)
  63. return Array.apply(null, { length: _count }).map((_, _i) => DayDetail(year, month, _i + 1))
  64. }
  65. const MonthDaysDetailFull = (year, month) => {
  66. const _days = MonthDaysDetail(year, month)
  67. const _beforeDays = Array.apply(null, { length: _days[0].week }).map((_, _i) => DayDetail(year, month, -_i, 'prev')).reverse()
  68. const _lastDay = _days[_days.length - 1]
  69. const _afterDays = Array.apply(null, { length: 6 - _lastDay.week }).map((_, _i) => DayDetail(year, month, _lastDay.day + _i + 1, 'next'))
  70. return {
  71. count: _days.length,
  72. days: _beforeDays.concat(_days).concat(_afterDays)
  73. }
  74. }
  75. const CorrectDate = (gregorianYear, gregorianMonth, gregorianDay) => {
  76. const _date = new Date(gregorianYear, gregorianMonth - 1, gregorianDay)
  77. return { year: _date.getFullYear(), month: _date.getMonth() + 1, day: _date.getDate() }
  78. }
  79. const WeekFirstDay = (gregorianYear, gregorianMonth, gregorianDay) => {
  80. const _date = new Date(gregorianYear, gregorianMonth - 1, gregorianDay)
  81. const week = _date.getDay()
  82. const wf = new Date(_date.getFullYear(), _date.getMonth(), _date.getDate() - week)
  83. return { year: wf.getFullYear(), month: wf.getMonth() + 1, day: wf.getDate() }
  84. }
  85. const YearWeekOrder = (gregorianYear, gregorianMonth, gregorianDay) => {
  86. const currDate = new Date(gregorianYear, gregorianMonth - 1, gregorianDay)
  87. const firstDate = new Date(gregorianYear, 0, 1)
  88. const diff = Math.round((currDate.valueOf() - firstDate.valueOf()) / 86400000)
  89. return Math.ceil((diff + ((firstDate.getDay() + 1) - 1)) / 7)
  90. }
  91. module.exports = {
  92. Weeks,
  93. Today,
  94. DayDetail,
  95. MonthDaysCount,
  96. MonthDaysDetail,
  97. MonthDaysDetailFull,
  98. initCurrInfo,
  99. CorrectDate,
  100. LunarDetail,
  101. WeekFirstDay,
  102. YearWeekOrder,
  103. Months,
  104. MonthOnly,
  105. EnOrders,
  106. ConsoleStyle
  107. }