当前位置:网站首页>微信小程序获取年月日周及早上、中午、晚上
微信小程序获取年月日周及早上、中午、晚上
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( ); //获取日期与时间边栏推荐
- Legal education combined with VR panorama, intuitively feel and learn the spirit of the rule of law
- 解决dataset.mnist无法加载进去的情况
- MySQL当前读、快照读、MVCC
- DocuWare Platform - Content Services and Workflow Automation Platform for Document Management (Part 1)
- 不需要服务器,教你仅用30行代码搞定实时健康码识别
- 我说MySQL联合索引遵循最左前缀匹配原则,面试官让我回去等通知
- Summary of some pytorch knowledge points that have been updated for a long time
- What are the useful IT asset management platforms?
- GPS卫星同步时钟,NTP网络同步时钟,北斗时钟服务器(京准)
- 吴恩达机器学习[13]-支持向量机
猜你喜欢

Xi'an Zongheng Information × JNPF: Adapt to the characteristics of Chinese enterprises, fully integrate the cost management and control system

Redis持久化操作

For循环控制

面渣逆袭:MySQL六十六问,两万字+五十图详解

DocuWare平台——用于文档管理的内容服务和工作流自动化的平台(上)

洛谷题解P4326 求圆的面积

B 站又上热搜了, HR 称「核心用户都是 Loser」

#夏日挑战赛# HarmonyOS 实现一个滑块验证

Projector reached the party benefits 】 【 beginners entry - brightness projection and curtain selection - from entry to the master

In action: 10 ways to implement delayed tasks, with code!
随机推荐
ITSM软件与工单系统的区别是什么?
(2022杭电多校五)C - Slipper (dijkstra+虚拟结点)
inter-process communication
全差分运放:THS4140
西安纵横资讯×JNPF:适配中国企业特色,全面集成费用管控体系
素士科创板IPO撤单,雷军失去“电动牙刷第一股”
24、shell编程-流程控制
保证通信的机制有哪些
DevOps平台中的制品库是什么?有什么用处?
The electromagnetic compatibility EMC protection study notes
#夏日挑战赛# HarmonyOS 实现一个滑块验证
分布式链路追踪Jaeger + 微服务Pig在Rainbond上的实践分享
Redis持久化操作
全球电子产品需求放缓,三星手机越南工厂每周只需要干 3~4 天
Unity AR阴影投射透明地面 仅渲染模型实时阴影 Shader实现
我说MySQL联合索引遵循最左前缀匹配原则,面试官让我回去等通知
Codeforces Round #811 A~F
界面组件DevExpress ASP.NET Core v22.1 - 增强数据导出功能
游戏网络 UDP+FEC+KCP
What is the difference between member variable and local variable