getDate() {
var date = new Date() // 获取时间
var year = date.getFullYear() // 获取年
var month = date.getMonth() + 1 // 获取月
var strDate = date.getDate() // 获取日
// var day = date.getDate() //
var day = ‘日一二三四五六’.charAt(new Date().getDay()) // 周一返回的是1,周六是6,但是周日是0
var hour = date.getHours() // 获取小时
var minute = date.getMinutes() // 获取分钟
var second = date.getSeconds() // 获取秒
// 由于部分业务处理 是需要月份日份前面有0 故新增一个函数
this.getNum()
console.log(‘今天星期’ + day)
console.log(
year +
‘-’ +
this.getNum(month) +
‘-’ +
this.getNum(strDate) +
‘_’ +
hour +
‘-’ +
minute +
‘-’ +
second
)
},
getNum(i) {
return i < 10 ? ‘0’ + i : i
},
// 再加一个定时器每秒获取
getTime() {
setInterval(() => {
this.getDate()
}, 1000)
},
原文链接:https://blog.csdn.net/ArtGhosts/article/details/126436007
dayJS获取时间
获取当前月份
dayjs().month() //当前为2022年11月份,输出10,因为月份从0开始。
设置月份
dayjs().month(0) //Fri Jan 14 2022 19:07:48
const pickerOptions = {
shortcuts: [
{
text: “本周”,
onClick(picker) {
const end = dayjs().endOf(“week”).add(1, “day”).valueOf();
const start = dayjs().startOf(“week”).add(1, “day”).valueOf();
picker.KaTeX parse error: Expected 'EOF', got '}' at position 35: …, end]); }̲, }, { …emit(“pick”, [start, end]);
},
},
{
text: “季度”,
onClick(picker) {
const start = dayjs().startOf(“quarter”).valueOf();
const end = dayjs().endOf(“quarter”).valueOf();
picker.KaTeX parse error: Expected 'EOF', got '}' at position 35: …, end]); }̲, }, { …emit(“pick”, [start, end]);
},
},
{
text: “自然年”,
onClick(picker) {
const start = dayjs().startOf(“year”).valueOf();
const end = dayjs().endOf(“year”).valueOf();
picker.KaTeX parse error: Expected 'EOF', got '}' at position 35: …, end]); }̲, }, { …emit(“pick”, [start, end]);
},
},
{
text: “最近30天”,
onClick(picker) {
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
picker.KaTeX parse error: Expected 'EOF', got '}' at position 35: …, end]); }̲, }, { …emit(“pick”, [start, end]);
},
},
{
text: “最近180天”,
onClick(picker) {
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 90 * 2);
picker.KaTeX parse error: Expected 'EOF', got '}' at position 35: …, end]); }̲, }, { …emit(“pick”, [start, end]);
},
},
],
};
转载:https://www.jianshu.com/p/2b91a49f552c