在微信小程序JS中自定义获取当前日期函数,并将自定义参数传递给前端
时间: 2024-03-25 21:35:51 浏览: 23
在微信小程序JS中自定义获取当前日期函数,并将自定义参数传递给前端,可以通过以下方式实现:
```
function getCurrentDate(format) {
// 获取当前日期
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth() + 1;
const day = today.getDate();
// 根据传入的格式化参数返回日期字符串
if (format === 'yyyy-mm-dd') {
return year + '-' + month + '-' + day;
} else if (format === 'mm/dd/yyyy') {
return month + '/' + day + '/' + year;
} else {
return year + '/' + month + '/' + day;
}
}
Page({
data: {
currentDate: ''
},
onLoad: function () {
// 调用自定义函数获取当前日期
const currentDate = getCurrentDate('yyyy-mm-dd');
// 将日期传递给前端
this.setData({
currentDate: currentDate
});
}
})
```
在这个例子中,我们定义了一个名为getCurrentDate的函数,用来获取当前日期,并根据传入的format参数返回不同格式的日期字符串。在页面加载时,我们调用该函数获取当前日期,并将其传递给前端。在这个例子中,我们传入了'yyyy-mm-dd'作为format参数,因此返回的日期字符串格式为'年-月-日'。在前端页面中,我们可以使用{{currentDate}}来显示当前日期。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)