index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. Component({
  2. /**
  3. * 组件的属性列表
  4. */
  5. properties: {},
  6. /**
  7. * 组件的初始数据
  8. */
  9. data: {
  10. statusBarHeight: 0,
  11. screenHeight: 0,
  12. customHeight: 0,
  13. bottomNavHeight: 0,
  14. contentHeight: 0,
  15. date_start: '',
  16. date_end: '',
  17. daysBetween:1,
  18. show: false,
  19. },
  20. lifetimes: {
  21. detached: function () {},
  22. attached: async function () {
  23. const systemInfo = wx.getSystemInfoSync();
  24. const statusBarHeight = systemInfo.statusBarHeight || 0;
  25. const screenHeight = systemInfo.screenHeight || 0;
  26. const custom = wx.getMenuButtonBoundingClientRect();
  27. const customHeight = custom.height + 10 + 2 || 0;
  28. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  29. const contentHeight = (screenHeight - bottomNavHeight - 50 - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  30. this.setData({
  31. statusBarHeight,
  32. screenHeight,
  33. customHeight,
  34. bottomNavHeight,
  35. contentHeight
  36. });
  37. this.getcurrentdate();
  38. },
  39. },
  40. /**
  41. * 组件的方法列表
  42. */
  43. methods: {
  44. //获取今日明日日期
  45. getcurrentdate() {
  46. const today = new Date();
  47. const tomorrow = new Date();
  48. tomorrow.setDate(today.getDate() + 1);
  49. this.setData({
  50. date_start: this.formatDate(today),
  51. date_end: this.formatDate(tomorrow),
  52. });
  53. console.log(this.data.date_start, this.data.date_end);
  54. },
  55. // 计算两个日期之间的天数
  56. calculateDaysBetween(startDate, endDate) {
  57. const start = new Date(startDate);
  58. const end = new Date(endDate);
  59. const timeDifference = end - start; // 计算时间差(毫秒)
  60. const daysDifference = timeDifference / (1000 * 3600 * 24); // 转换为天数
  61. return daysDifference; // 返回天数差
  62. },
  63. //开日历
  64. onDisplay() {
  65. this.setData({
  66. show: true
  67. });
  68. },
  69. //关日历
  70. onClose() {
  71. this.setData({
  72. show: false
  73. });
  74. },
  75. //转换日期
  76. formatDate(date) {
  77. date = new Date(date);
  78. return `${date.getMonth() + 1}月${date.getDate()}日`;
  79. },
  80. //选好日期点击完成后
  81. onConfirm(event) {
  82. const [start, end] = event.detail;
  83. const daysBetween = this.calculateDaysBetween(start, end); // 计算天数差
  84. this.setData({
  85. show: false,
  86. date_start: `${this.formatDate(start)} `,
  87. date_end: `${this.formatDate(end)}`,
  88. daysBetween
  89. });
  90. console.log(`入住日期: ${this.data.date_start}, 离店日期: ${this.data.date_end}, 天数差: ${daysBetween}天`);
  91. },
  92. gourl(e){
  93. const url = e.currentTarget.dataset.url
  94. wx.navigateTo({
  95. url: `${url}`,
  96. })
  97. }
  98. }
  99. });