当前位置:网站首页>微信小程序获取年月日周及早上、中午、晚上
微信小程序获取年月日周及早上、中午、晚上
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( ); //获取日期与时间边栏推荐
- 【Harmony OS】【FAQ】鸿蒙问题合集2
- DocuWare平台——用于文档管理的内容服务和工作流自动化的平台(上)
- 解决dataset.mnist无法加载进去的情况
- 素士科创板IPO撤单,雷军失去“电动牙刷第一股”
- #夏日挑战赛# HarmonyOS 实现一个滑块验证
- DocuWare Platform - Content Services and Workflow Automation Platform for Document Management (Part 1)
- Go Go 简单的很,标准库之 fmt 包的一键入门
- GPS卫星同步时钟,NTP网络同步时钟,北斗时钟服务器(京准)
- 如何优雅的消除系统重复代码?
- 云存储硬核技术内幕——(8) 只缘身在此山中
猜你喜欢
随机推荐
tif转mat
Why, when you added a unique index or create duplicate data?
从-99打造Sentinel高可用集群限流中间件
西安纵横资讯×JNPF:适配中国企业特色,全面集成费用管控体系
qt 复杂界面信号槽设计
Summary of some pytorch knowledge points that have been updated for a long time
H5 开发内嵌页面跨域问题
洛谷题解P4326 求圆的面积
PHP 图片转PDF
分布式链路追踪Jaeger + 微服务Pig在Rainbond上的实践分享
(2022杭电多校五)C - Slipper (dijkstra+虚拟结点)
Go 事,如何成为一个Gopher ,并在7天找到 Go 语言相关工作,第1篇
你一定从未看过如此通俗易懂的YOLO系列(从v1到v5)模型解读
Byte、Short、Integer、Long内部缓存类的对比与源码分析
初学爬虫笔记(收集数据)
《电磁兼容防护EMC》学习笔记
Beginner crawler notes (collecting data)
【Harmony OS】【FAQ】Hongmeng Questions Collection 2
皕杰报表配置文件report_config.xml里都配置了什么?
邮差"头":{“retCode”:“999999”








