当前位置:网站首页>微信小程序获取年月日周及早上、中午、晚上
微信小程序获取年月日周及早上、中午、晚上
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( ); //获取日期与时间边栏推荐
- 实战:10 种实现延迟任务的方法,附代码!
- The electromagnetic compatibility EMC protection study notes
- Byte、Short、Integer、Long内部缓存类的对比与源码分析
- 我说MySQL联合索引遵循最左前缀匹配原则,面试官让我回去等通知
- postman “header“:{“retCode“:“999999“
- Projector reached the party benefits 】 【 beginners entry - brightness projection and curtain selection - from entry to the master
- Codeforces Round #811 A~F
- 【Harmony OS】【FAQ】鸿蒙问题合集2
- 初学爬虫笔记(收集数据)
- Roslyn 在多开发框架让 msbuild 的 Target 仅运行一次
猜你喜欢
随机推荐
MySQL当前读、快照读、MVCC
界面组件DevExpress ASP.NET Core v22.1 - 增强数据导出功能
我在羊毛和二手群里报复性消费
H5 开发内嵌页面跨域问题
qt 复杂界面信号槽设计
Roslyn 在 msbuild 的 target 判断文件存在
tif转mat
一文详解什么是软件部署
RSA306B,500,600系列API接口代码
攻防视角下,初创企业安全实战经验分享
06-总线
JVM调优-GC基本原理和调优关键分析
Tinymce plugins [Tinymce 扩展插件集合]
dot net double 数组转 float 数组
Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
GPS卫星同步时钟,NTP网络同步时钟,北斗时钟服务器(京准)
Flutter 运动鞋商铺小demo
Typora收费?搭建VS Code MarkDown写作环境
Go Go 简单的很,标准库之 fmt 包的一键入门
What is the difference between ITSM software and a work order system?







![吴恩达机器学习[9]-神经网络学习](/img/07/0eeb3cd5f3ea7c2baeec1732ea8d9a.png)

