123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- Component({
- /**
- * 组件的属性列表
- */
- properties: {},
- /**
- * 组件的初始数据
- */
- data: {
- statusBarHeight: 0,
- screenHeight: 0,
- customHeight: 0,
- bottomNavHeight: 0,
- contentHeight: 0,
-
- date_start: '',
- date_end: '',
- daysBetween:1,
- show: false,
- },
- lifetimes: {
- detached: function () {},
- attached: async function () {
- const systemInfo = wx.getSystemInfoSync();
- const statusBarHeight = systemInfo.statusBarHeight || 0;
- const screenHeight = systemInfo.screenHeight || 0;
- const custom = wx.getMenuButtonBoundingClientRect();
- const customHeight = custom.height + 10 + 2 || 0;
- const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
- const contentHeight = (screenHeight - bottomNavHeight - 50 - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
- this.setData({
- statusBarHeight,
- screenHeight,
- customHeight,
- bottomNavHeight,
- contentHeight
- });
- this.getcurrentdate();
- },
- },
- /**
- * 组件的方法列表
- */
- methods: {
- //获取今日明日日期
- getcurrentdate() {
- const today = new Date();
- const tomorrow = new Date();
- tomorrow.setDate(today.getDate() + 1);
-
- this.setData({
- date_start: this.formatDate(today),
- date_end: this.formatDate(tomorrow),
- });
- console.log(this.data.date_start, this.data.date_end);
- },
- // 计算两个日期之间的天数
- calculateDaysBetween(startDate, endDate) {
- const start = new Date(startDate);
- const end = new Date(endDate);
- const timeDifference = end - start; // 计算时间差(毫秒)
- const daysDifference = timeDifference / (1000 * 3600 * 24); // 转换为天数
- return daysDifference; // 返回天数差
- },
- //开日历
- onDisplay() {
- this.setData({
- show: true
- });
- },
- //关日历
- onClose() {
- this.setData({
- show: false
- });
- },
- //转换日期
- formatDate(date) {
- date = new Date(date);
- return `${date.getMonth() + 1}月${date.getDate()}日`;
- },
- //选好日期点击完成后
- onConfirm(event) {
- const [start, end] = event.detail;
- const daysBetween = this.calculateDaysBetween(start, end); // 计算天数差
- this.setData({
- show: false,
- date_start: `${this.formatDate(start)} `,
- date_end: `${this.formatDate(end)}`,
- daysBetween
- });
- console.log(`入住日期: ${this.data.date_start}, 离店日期: ${this.data.date_end}, 天数差: ${daysBetween}天`);
- },
- gourl(e){
- const url = e.currentTarget.dataset.url
- wx.navigateTo({
- url: `${url}`,
- })
- }
- }
- });
|