当前位置:网站首页>微信小程序获取年月日周及早上、中午、晚上
微信小程序获取年月日周及早上、中午、晚上
2022-08-04 15:41:00 【coldriversnow】
首先我们在utils文件 中新建一个js文件,然后把当前时间和时间段的代码写在这个文件中。代码就如下:
function formatTime(date) {
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
return year + "年" + month + "月" + day + "日";
}
const formatDay = dates => {
let _day = new Array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');
let date = new Date(dates);
date.setDate(date.getDate());
let day = date.getDay();
return _day[day];
}
const formatSole = () => {
let timeNow = new Date();
let hours = timeNow.getHours();
let text = ``;
if (hours >= 0 && hours <= 6) {
text = `深夜了,不要熬夜太久哟`;
} else if (hours > 6 && hours <= 8) {
text = `早上好`;
} else if (hours > 8 && hours <= 10) {
text = `上午好`;
} else if (hours > 10 && hours <= 13) {
text = `中午好`;
} else if (hours > 13 && hours <= 17) {
text = `下午好`;
} else if (hours > 17 && hours <= 23) {
text = `晚上好`;
}
return text;
}
module.exports = {
formatTime: formatTime,
formatDay: formatDay,
formatSole: formatSole
}
然后再项目页面中的js文件引入该js文件,我的需求是页面加载后显示,代码如下:
var util = require('../../utils/time.js')
Page({
data: {
promptTime:'',
promptDay:'',
promptTimeSole:'',
},
onLoad: function(options) {
this.setData({
promptTime: util.formatTime(new Date()),
promptDay: util.formatDay(new Date()),
promptTimeSole: util.formatSole(),
});
},
})
这样就能实现在页面中显示当前年-月-日-周 时间段了。效果图如下:
{
{promptTime}}{
{promptDay}}{
{promptTimeSole}}
微信小程序获取当前时间/年月日
var myDate = new Date();
myDate.getYear(); //获取当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-????)
myDate.getMonth(); //获取当前月份(0-11,0代表1月)
myDate.getDate(); //获取当前日(1-31)
myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours(); //获取当前小时数(0-23)
myDate.getMinutes(); //获取当前分钟数(0-59)
myDate.getSeconds(); //获取当前秒数(0-59)
myDate.getMilliseconds(); //获取当前毫秒数(0-999)
myDate.toLocaleDateString(); //获取当前日期
var mytime=myDate.toLocaleTimeString(); //获取当前时间
myDate.toLocaleString( ); //获取日期与时间
边栏推荐
猜你喜欢
随机推荐
H5 之 文件流转base64下载
Jupyter常用操作总结(强烈建议收藏,持续更新实用操作)
农产品期货开户哪家好??
【愚公系列】2022年07月 Go教学课程 028-函数小结案例(通讯录)
2022杭电多校3
录音文件识别
Codeforces Round #811 A~F
An article to answer what is the product library of the DevOps platform
inter-process communication
dot net core 使用 usb
什么是 DevOps?看这一篇就够了!
Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
Task Computing【动态规划_牛客】
JVM Tuning-GC Fundamentals and Tuning Key Analysis
云存储硬核技术内幕——(9) 相见时难别亦难
Many merchants mall system function and dismantling 24 - ping the strength distribution of members
Manacher(求解最长回文子串)
吴恩达机器学习[9]-神经网络学习
Typora收费?搭建VS Code MarkDown写作环境
我说MySQL联合索引遵循最左前缀匹配原则,面试官让我回去等通知