当前位置:网站首页>微信小程序获取年月日周及早上、中午、晚上
微信小程序获取年月日周及早上、中午、晚上
2022-07-17 05:05:00 【小青年一枚】
好久没写笔记了,今天简单的记录下在开发小程序时,遇到了页面获取当前时间和时间段,记录总结以下。
首先我们在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(),
});
},
}) 这样就能实现在页面中显示当前年-月-日-周 时间段了。效果图如下:

边栏推荐
猜你喜欢

Use of flask

第十届泰迪杯数据挖掘挑战赛A题害虫识别YOLOv5模型代码(已跑通,原创作品,持续更新)

ModelArts第二次培訓筆記

Simple use of directexchange switches.

【C语言_复习_学习第二课】什么是进制?进制之间应该如何转换

读论文《Learning to Measure Changes: Fully Convolutional Siamese Metric Networks for Scene Change Detec》

C语言初学者之初识代码专项练习

Harmonyos fourth training notes

Shallow chat link tracking

645. 错误的集合
随机推荐
The difference between junit4 and junit5
Add SSL certificate for load balancing
User management - paging
上传七牛云的方法
一个问题的探讨
Harmonyos second training notes
Order system of middle office
IDL 读取葵花8(Himawari-8)HSD数据
【C语言—零基础第九课】函数中的爱恨情仇
日志加入数据库实现思路
C语言初学者之初识代码专项练习
HarmonyOS第三次培训笔记
用户的管理-限制
02 Bar _ Recommandation de film (basée sur le contenu) Portrait de l'utilisateur
【C语言—零基础_学习_复习_第四课】数据类型及其运算
POC——DVWA‘s File Upload
Learn about scheduled tasks in one article
Swagger's pit
数据分析与数据挖掘实战案例本地房价预测(716):
Install MySQL