vue:日期的相关方法

vue:日期的相关方法

项目里用到挺多与日期相关的逻辑,现在记录一下我用到过的方法。

目录

1、获取当天日期,格式:yyyy-MM-dd

2、将系统默认时间(例如Wed Jun 19 2019 15:33:53 GMT+0800 (新加坡标准时间))转为格式为yyyy-MM-dd的日期

3.获取某一天的前几天或后几天

4.判断提供的日期是工作日还是双休日

5. 判断指定日期是否超出当天

6. 获取指定日期的前(后)几日、几月、几年

7. 获取当天为星期几

8. 时间戳 转 yyyy-mm-dd hh-mm-ss

9. yyyy-mm-dd hh-mm-ss 转 时间戳

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)

}

欢迎补充和纠错~

相关阅读

佛教告诉我们的真理都有哪些
365bet官方投注网址

佛教告诉我们的真理都有哪些

🕒 07-07 👁️‍🗨️ 5139
中华蝾螈吃什么,主要食物是小型的动物
365bet官网赌场

中华蝾螈吃什么,主要食物是小型的动物

🕒 07-30 👁️‍🗨️ 5870
华山长空栈道路线图:勇者的挑战,绝壁之上的惊险之旅
久发365电子游戏网址多少

华山长空栈道路线图:勇者的挑战,绝壁之上的惊险之旅

🕒 07-24 👁️‍🗨️ 6949
vikoo绘客手绘数位板怎么样好不好用?绘客t50真实使用测评
365bet官方投注网址

vikoo绘客手绘数位板怎么样好不好用?绘客t50真实使用测评

🕒 07-31 👁️‍🗨️ 629
iPhone微信小视频保存到本地的三种方法_iphone指南
365bet官网赌场

iPhone微信小视频保存到本地的三种方法_iphone指南

🕒 08-12 👁️‍🗨️ 3908
12生肖感恩的动物:探索生肖中的感恩之情
365bet官方投注网址

12生肖感恩的动物:探索生肖中的感恩之情

🕒 07-01 👁️‍🗨️ 8946
高大上!奥巴马乘空军一号目睹美国输球2014-06-27 09:52:43 来自:爱奇艺
唱吧隐私设置在哪里
久发365电子游戏网址多少

唱吧隐私设置在哪里

🕒 07-15 👁️‍🗨️ 8040
鏖怎么读
365bet官网赌场

鏖怎么读

🕒 06-30 👁️‍🗨️ 7503