首页
视频
资源
登录
原
js 获取当天的年月日 包含0的格式,自动补0。js获取今日,昨日,前日
1945
人阅读
2023/4/3 16:58
总访问:
337280
评论:
0
收藏:
0
手机
分类:
前端
### 代码如下: ``` var date = new Date() // 获取时间 var year = date.getFullYear() // 获取年 var month = date.getMonth() + 1 // 获取月 var strDate = date.getDate() // 获取日 // 自动补0 if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } let typeDayValue = '' //今日 if (this.typeDay == "today") { typeDayValue = year + "-" + month + "-" + strDate; } //昨日 if (this.typeDay == "yesterday") { typeDayValue = year + "-" + month + "-" + (strDate - 1); } //前日 if (this.typeDay == "beforeYesterday") { typeDayValue = year + "-" + month + "-" + (strDate - 2); } ``` ### 上面的只有当天的日期,会补零,昨日前日要补零还是要加一个方法 ``` mounted() { var date = new Date() // 获取时间 var year = date.getFullYear() // 获取年 var month = date.getMonth() + 1 // 获取月 var strDate = date.getDate() // 获取日 // 自动补0 // if (month >= 1 && month <= 9) { // month = "0" + month; // } // if (strDate >= 0 && strDate <= 9) { // strDate = "0" + strDate; // } let typeDayValue = '' //今日 if (this.typeDay == "today") { typeDayValue = year + "-" + this.addZero(month) + "-" + this.addZero(strDate); } //昨日 if (this.typeDay == "yesterday") { typeDayValue = year + "-" + this.addZero(month) + "-" + this.addZero(strDate - 1); } //前日 if (this.typeDay == "beforeYesterday") { typeDayValue = year + "-" + this.addZero(month) + "-" + this.addZero(strDate - 2); } this.typeDayStr = typeDayValue this.GetLabroomLearnActivityDetails(typeDayValue, 1) this.getLearnActivityDetails(typeDayValue, 1) this.GetEvalLearnActivityDetails(typeDayValue, 1) }, // 组件方法 methods: { // 自动补零 addZero(val) { if (val >= 1 && val <= 9) { val = "0" + val; } return val } } ```
👈{{preArticle.title}}
👉{{nextArticle.title}}
评价
{{titleitem}}
{{titleitem}}
{{item.content}}
{{titleitem}}
{{titleitem}}
{{item.content}}
小见
吃亏决不亏,惜福才有福
博主信息
排名
6
文章
6
粉丝
16
评论
8
文章类别
云服务
10篇
计算机基础
2篇
NET
5篇
.NET
7篇
后端
4篇
前端
24篇
EF
4篇
随笔
5篇
数据库
1篇
.net core
3篇
linux
2篇
微服务
1篇
移动开发
1篇
docker
1篇
最新文章
最新评价
{{item.articleTitle}}
{{item.blogName}}
:
{{item.content}}
关于我们
ICP备案 :
渝ICP备18016597号-1
网站信息:
2018-2024
TNBLOG.NET
技术交流:
群号656732739
联系我们:
contact@tnblog.net
欢迎加群
欢迎加群交流技术