一、 20230908
方法①:
let now = new Date();
let y = now.getFullYear();
let m = ("0" + (now.getMonth() + 1)).slice(-2);
let d = ("0" + now.getDate()).slice(-2);
let nowDate = y+m+d; //打印为20230908
方法②:
使用moment.js
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
let nowDate = moment().startOf("day").format("YYYYMMDD");//20230908
二、2023-09-08 12:20:01
方法①:
function selectTime(){
let time = new Date().getTime();
var date = new Date(time + 8*3600*1000);
return date.toJSON().substr(0, 19).replace('T', ' ');//2023-09-08 12:20:01
}
selectTime()
方法②:
使用moment.js
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
let nowTime = moment().format('YYYY-MM-DD HH:mm:ss');//2023-09-08 12:20:01
方法③:封装讲new Date()转换格式为"yyyy-MM-dd HH:mm:ss"
// 通过new Date获取当前时间
let a = new Date('2019-01-24T16:35:01.000+0800')
console.log(a); //Thu Jan 24 2019 16:35:01 GMT+0800 (中国标准时间)
在Date内置对象身上自定义封装一个转换的函数如下:
public.js (单独写在一个js文件中)
Date.prototype.Format = function (fmt) {
var o= {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"H+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
main.js (在入口文件引入)
import '@/api/public.js'
组件中使用:
created() {
let a = new Date('2019-01-24T16:35:01.000+0800')//给定时间
console.log(a); //Thu Jan 24 2019 16:35:01 GMT+0800 (中国标准时间)
let b = a.Format("yyyy-MM-dd HH:mm:ss")
console.log(b); //2019-01-24 16:35:01
let c = new Date()//当前时间
console.log(c); //Thu Mar 24 2022 17:17:34 GMT+0800 (中国标准时间)
let d = c.Format("yyyy-MM-dd HH:mm:ss")
console.log(d); //2022-03-24 17:16:31
},
三、9月8日
// 需要引入moment.js
`${moment(new Date().getTime()).format("M")}月` +
`${moment(new Date().getTime()).format("D")}日`;
// 9月8日
四、周几
// 需要引入moment.js
getCurWeek() {
let weekDay = moment(new Date().getTime()).format("e");
if (weekDay == 0) {
this.curWeek = "周日";
} else if (weekDay == 1) {
this.curWeek = "周一";
} else if (weekDay == 2) {
this.curWeek = "周二";
} else if (weekDay == 3) {
this.curWeek = "周三";
} else if (weekDay == 4) {
this.curWeek = "周四";
} else if (weekDay == 5) {
this.curWeek = "周五";
} else if (weekDay == 6) {
this.curWeek = "周六";
}
return this.curWeek;
}
五、16:43:30
需要引入moment.js
moment().format("HH:mm:ss")
六、获取当前周周一日期、当前月一号日期
比如今天是2023-09-07:
本周就是2023-09-04 ~ 2023-09-07;
本月就是2023-09-01~2023-09-07;
思路:获取今日、获取星期一日期、获取本月第一天日期
获取今日:
let today = new Date() //Fri Sep 08 2023 17:57:05 GMT+0800 (中国标准时间)
let nowDayOfWeek = current.getDay(); // 今天是本周的第几天
let day = nowDayOfWeek || 7;
获取本周的周一日期:
let weekDay = new Date(
current.getFullYear(),
current.getMonth(),
current.getDate() + 1 - day //------>计算当前周周一是几号
); // 本周
let monthDay = new Date(current.getFullYear(), current.getMonth(), 1); // 当前月1号