js时间方法-两日期串的天数之差-获取特定时间的几天前后时间

// 时间戳转时间
timestampToTime(timestamp) {
    var date = new Date(timestamp);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = (date.getDate() + 1 < 10 ? '0' + (date.getDate()) : date.getDate());
    return Y + M + D;
},
// 时间转时间戳
functiontransdate(dateValue) {
    var date = new Date();
    date.setFullYear(dateValue.substring(0, 4));
    date.setMonth(dateValue.substring(5, 7) - 1);
    date.setDate(dateValue.substring(8, 10));
    return Date.parse(date);

},
//两日期串的天数之差, 前-后, sDate1-sDate2
function DateDiff(sDate1, sDate2) { //sDate1和sDate2是"2002-12-18"格式
  var aDate, oDate1, oDate2, iDays;
  aDate = sDate1.split("-");
  oDate1 = new Date(aDate[0], aDate[1] - 1, aDate[2]);
  aDate = sDate2.split("-");
  oDate2 = new Date(aDate[0], aDate[1] - 1, aDate[2]);
  iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24);
  if ((oDate1 - oDate2) < 0) {
    return -iDays;
  }
  return iDays;
}
//两日期串的天数之差, 前-后, sDate1-sDate2
function DateDiff2(sDate1, sDate2) { //sDate1和sDate2是"12/18/2011"格式
  var oDate1, oDate2, iDays;
  oDate1 = new Date(sDate1);
  oDate2 = new Date(sDate2);
  var iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24);
  if ((oDate1 - oDate2) < 0){
    return -iDays;
  }
  return iDays;
}
 /**
    *获取特定时间的几天前后时间
    *{param:date} date 输入日期(YYYY-MM-DD)
    *{param:day } 天数
*/
getNextDate(date, day) {
    var dd = new Date(date);
    dd.setDate(dd.getDate() + day);
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
    var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
    return y + "-" + m + "-" + d;
},
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值