当前位置:网站首页>自定义时间格式(YYYY-MM-DD HH:mm:ss 星期X)
自定义时间格式(YYYY-MM-DD HH:mm:ss 星期X)
2022-04-23 06:24:00 【资深咸鱼~】
<script>
function format(newDate) {
var day = newDate.getDay()
var y = newDate.getFullYear()
var m = (newDate.getMonth() + 1) < 10 ? "0" + (newDate.getMonth() + 1) : (newDate.getMonth() + 1)
var d = newDate.getDate() < 10 ? "0" + newDate.getDate() : newDate.getDate()
var h = newDate.getHours() < 10 ? "0" + newDate.getHours() : newDate.getHours()
var min = newDate.getMinutes() < 10 ? "0" + newDate.getMinutes() : newDate.getMinutes()
var s = newDate.getSeconds() < 10 ? "0" + newDate.getSeconds() : newDate.getSeconds()
var dict = {
1: "一",
2: "二",
3: "三",
4: "四",
5: "五",
6: "六",
0: "天",
}
//var week=["日","一","二","三","四","五","六"]
return y + "-" + m + "-" + d + " " + h + ":" + min + ":" + s + " 星期" + dict[day]
}
var newDate = new Date()
console.log(format(newDate))
</script>
注意点:
一、date.getDay()的结果为数字0-6,0对应星期天、1-6对应星期一到星期六
将数字转化为中文,有两种方法:
(1)字典 var dict = {
1: "一",
2: "二",
3: "三",
4: "四",
5: "五",
6: "六",
0: "天",
}
使用:dict[date.getDay()]
(2)数组 var week=["日","一","二","三","四","五","六"]
使用:week[date.getDay()]
二、date.getMonth()得到的月份+1才为真实月份
打印结果

版权声明
本文为[资深咸鱼~]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_51644702/article/details/123920756
边栏推荐
- vim+ctags+cscpope开发环境搭建指南
- 什么是闭包?
- kaggle-房价预测实战
- 如何SQL 语句UNION实现当一个表中的一列内容为空时则取另一个表的另一列
- [CodeForces - 208E] Blood Cousins(k代兄弟问题)
- 菜菜的刷题日记 | 蓝桥杯 — 十六进制转八进制(纯手撕版)附进制转换笔记
- 菜菜的并发编程笔记 |(五)线程安全问题以及Lock解决方案
- 免费开源农业物联网云平台(Version:3.0.1)
- Object.create()原理,Object.create()规范,手写Object.create(),Object.create()用法
- Solution of emergency communication system for major security incidents
猜你喜欢

Jupyter Notebook 安装

SDC intelligent communication patrol management system of Nanfang investment building

Background management system framework, there is always what you want

学习资料

Machine vision series (01) -- Overview

el-date-picker中自定义快捷选项picker-options,动态设置禁用日期

Educational Codeforces Round 81 (Rated for Div. 2)

DMR system solution of Kaiyuan MINGTING hotel of Fengqiao University

数据分析入门 | kaggle泰坦尼克任务(四)—>数据清洗及特征处理

菜菜的并发编程笔记 |(五)线程安全问题以及Lock解决方案
随机推荐
Jiangning hospital DMR system solution
获取字符格式的当前时间
presto日期函数的使用
Solution of emergency communication system for major security incidents
PyTorch 20. Pytorch tips (continuously updated)
Solution of wireless intercom system in Commercial Plaza
Patrol inspection intercom communication system in power industry
C语言的指针符号到底靠近变量类型还是变量名?
anaconda3安装
使用el-popconfirm和el-backtop不生效
华为云MVP邮件
Emergency communication system for flood control and disaster relief
SDC intelligent communication patrol management system of Nanfang investment building
ESP32学习-GPIO的使用与配置
Solution of self Networking Wireless Communication intercom system in Beifeng oil and gas field
推导式与正则式
hql求一个范围内最大值
可视化之路(十)分割画布函数详解
Educational Codeforces Round 81 (Rated for Div. 2)
PyTorch 13. Nested functions and closures (dog head)