vue:日期的相关方法
项目里用到挺多与日期相关的逻辑,现在记录一下我用到过的方法。
目录
2、将系统默认时间(例如Wed Jun 19 2019 15:33:53 GMT+0800 (新加坡标准时间))转为格式为yyyy-MM-dd的日期
1、获取当天日期,格式:yyyy-MM-dd
getCurrentDate(n) {
var dd = new Date();
if (n) {
dd.setDate(dd.getDate() - n);
}
var year = dd.getFullYear();
var month =
dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
var day = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
return year + "-" + month + "-" + day;
};
this.getCurrentDate():不带参数就是默认为当天日期
this.getCurrentDate(1):参数大于0,代表为之前的某一天
this.getCurrentDate(-1):参数小于0,代表为未来的某一天
2、将系统默认时间(例如Wed Jun 19 2019 15:33:53 GMT+0800 (新加坡标准时间))转为格式为yyyy-MM-dd的日期
formatterDate(date) {
if (!date) {
return "";
}
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
if (month < 10) {
month = "0" + month;
}
if (day < 10) {
day = "0" + day;
}
var nowDate = year + "-" + month + "-" + day;
return nowDate;
},
emmmm写完后发现两种方式好像差不多一样的。。。第一种方式更完善一些,这个方法是我再网上找的,然后自己改动了一下,本来想贴上找到的地址的,但是现在找不到了。。
-----------------
新增一个:
3.获取某一天的前几天或后几天
与第一个类似,不过第一个是当天的前几天或后几天,这一个是指定日期的前几天或后几天
两个参数,第一个参数为某一天日期(格式为yyyy-mm-dd),第二个参数为数字,正数表示该日期的后几天,负数为该日期的前几天
calculateDateTime(startDate,valueTime)=>{
var date = new Date(startDate);
var newDate = new Date(date.getFullYear(),date.getMonth(),date.getDate()+ +valueTime);
var Y = newDate.getFullYear();
var M = newDate.getMonth()+1;
M = M<10?'0'+M:M;
var D = newDate.getDate();
D = D<10?'0'+D:D;
return `${Y}-${M}-${D}`;
};
---------------------
4.判断提供的日期是工作日还是双休日
const isWeekday = (date) => date.getDay() % 6 !== 0;
// new Date(年,月,日) 0是1月,以此类推
isWeekday(new Date(2021, 0, 1)) => true // 是工作日
————————————————
11.12补充
5. 判断指定日期是否超出当天
let curDate = new Date(new Date().toLocaleDateString()).getTime() //当天
// date是传进来的日期yyyy-MM-dd
if ( new Date(date).getTime() < curDate ) {
// 过期
} else {
// 未过期
}
———————————————————
11.22补充
6. 获取指定日期的前(后)几日、几月、几年
第3个方法的完善
// date: 指定日期,格式为yyyy-MM-dd
// type: 天、周、月、年
// number: 几
// separator: 分隔符
function getDate(date, type=null,number=0, separator="") {
let nowdate = new Date(date);
let y ='',m='',d = ''
switch (type) {
case "day": //取number天前、后的时间
nowdate.setTime(nowdate.getTime() + (24 * 3600 * 1000) * number);
y = nowdate.getFullYear();
m = nowdate.getMonth() + 1 < 10 ? '0' + (nowdate.getMonth() + 1) : nowdate.getMonth() + 1;
d = nowdate.getDate() < 10 ? '0' + nowdate.getDate() : nowdate.getDate();
break;
case "week": //取number周前、后的时间
weekdate = new Date(nowdate + (7 * 24 * 3600 * 1000) * number);
y = weekdate.getFullYear();
m = weekdate.getMonth() + 1 < 10 ? '0' + (nowdate.getMonth() + 1) : nowdate.getMonth() + 1;
d = weekdate.getDate() < 10 ? '0' + nowdate.getDate() : nowdate.getDate();
break;
case "month": //取number月前、后的时间
nowdate.setMonth(nowdate.getMonth() + number);
y = nowdate.getFullYear();
m = nowdate.getMonth() + 1 < 10 ? '0' + (nowdate.getMonth() + 1) : nowdate.getMonth() + 1;
d = nowdate.getDate() < 10 ? '0' + nowdate.getDate() : nowdate.getDate();
break;
case "year": //取number年前、后的时间
nowdate.setFullYear(nowdate.getFullYear() + number);
y = nowdate.getFullYear();
m = nowdate.getMonth() + 1 < 10 ? '0' + (nowdate.getMonth() + 1) : nowdate.getMonth() + 1;
d = nowdate.getDate() < 10 ? '0' + nowdate.getDate() : nowdate.getDate();
break;
default: //取当前时间
y = nowdate.getFullYear();
m = nowdate.getMonth() + 1 < 10 ? '0' + (nowdate.getMonth() + 1) : nowdate.getMonth() + 1;
d = nowdate.getDate() < 10 ? '0' + nowdate.getDate() : nowdate.getDate();
}
return y + separator + m + separator + d;
}
-----------------------
1.9补充
7. 获取当天为星期几
fmtWeekDay(date){
if(date === "--"){
return "--";
}
let dt = new Date(date.split("-")[0], date.split("-")[1] - 1,date = date.split("-")[2]);
let weekDay = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
return weekDay[dt.getDay()];
},
参考链接JS获取当前时间的前几天、前几周、前几个月、前几年的时间_翩跹星子的博客-CSDN博客
-----------------------
5.31补充
8. 时间戳 转 yyyy-mm-dd hh-mm-ss
timestampToTime(stamp) {
let date = new Date(stamp);
let Y = date.getFullYear() + '-';
let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1):date.getMonth()+1) + '-';
let D = (date.getDate()< 10 ? '0'+date.getDate():date.getDate())+ ' ';
let h = (date.getHours() < 10 ? '0'+date.getHours():date.getHours())+ ':';
let m = (date.getMinutes() < 10 ? '0'+date.getMinutes():date.getMinutes()) + ':';
let s = date.getSeconds() < 10 ? '0'+date.getSeconds():date.getSeconds();
return Y + M + D + h + m + s;
}
9. yyyy-mm-dd hh-mm-ss 转 时间戳
timeToTimestamp(time) {
if (!time ) {
return ''
}
let timei = Date.parse(new Date(time)) + ''
return Number(timei)
}
欢迎补充和纠错~