uniapp打卡日期
获取当前日期
使用JavaScript的Date对象获取当前日期,格式化为YYYY-MM-DD形式:
const today = new Date();
const year = today.getFullYear();
const month = String(today.getMonth() + 1).padStart(2, '0');
const day = String(today.getDate()).padStart(2, '0');
const formattedDate = `${year}-${month}-${day}`;
存储打卡记录
通过uni.setStorageSync将打卡日期保存到本地存储:

uni.setStorageSync('checkInDate', formattedDate);
检查重复打卡
从本地存储读取历史记录,判断今日是否已打卡:

const savedDate = uni.getStorageSync('checkInDate');
if (savedDate === formattedDate) {
uni.showToast({ title: '今日已打卡', icon: 'none' });
} else {
// 执行打卡逻辑
}
显示日历组件
使用uni-calendar组件选择日期(需安装插件):
<uni-calendar @change="handleDateChange" />
methods: {
handleDateChange(e) {
console.log('选中日期:', e.fulldate);
}
}
后端交互示例
调用API提交打卡数据到服务器:
uni.request({
url: 'https://api.example.com/checkin',
method: 'POST',
data: { date: formattedDate },
success: (res) => {
uni.showToast({ title: '打卡成功' });
}
});
注意事项
- 本地存储适用于轻量数据,大量数据建议使用数据库。
- 日期比较时需考虑时区问题,建议统一使用UTC时间。
- 日历组件需通过HBuilderX插件市场安装。






