时间格式处理

// 当前年份
const FullYear=()=>{
    var myDate = new Date();
    return myDate.getFullYear(); 
}

//2017-01-01 转成 时间戳 123123123412
function changeTime(time) {
    return Date.parse(new Date(time));
  }

// 后端返回时间插件格式化
function postForMatDate(date) {
    let reg = /[-]/g;
    let reg1 = /[~]/g;
    let tiemDate1 = date;
  
    tiemDate1 = tiemDate1.replace(reg, '/');
    tiemDate1 = tiemDate1.replace(reg1, '-');
    return tiemDate1
  }

  // 时间插件格式化
  function getForMatDate(date) {
    let reg = /[/]/g;
    let reg1 = /[-]/g;
    let tiemDate1 = date;
  
    tiemDate1 = tiemDate1.replace(reg1, '~');
    tiemDate1 = tiemDate1.replace(reg, '-');
    return tiemDate1
  }

 // 时间戳转时间段
  function toDate(n, m) {
  //如果是毫秒的时间戳就不需要这一步,直接下一步就可以
    var date = new Date(n);
    var Y = date.getFullYear() + '';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '';
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  
    var date2 = new Date(m);
    var Y2 = date2.getFullYear() + '';
    var M2 = (date2.getMonth() + 1 < 10 ? '0' + (date2.getMonth() + 1) : date2.getMonth() + 1) + '';
    var D2 = date2.getDate() < 10 ? '0' + date2.getDate() : date2.getDate();
    return (Y + M + D + " - " + Y2 + M2 + D2);
  }


function dataToString(n, m) {
    var date1 = new Date(n);
    var data2 = new Date(m);
  
    function t(date) {
      var Y = date.getFullYear() + '-';
      var M = date.getMonth() + 1 < 10 ? ('0' + (date.getMonth() + 1)) : (date.getMonth() + 1) + '-';
      var D = date.getDate() < 10 ? ('0' + date.getDate() ) : data2.getDate();
      return Y + M + D;
    }
  
    return t(date1) + '~' + t(data2)
  }


// 从今日向后推一周
// 从今日向后推七周

function gethebdomad(){
  const end = new Date();
  const start = new Date();
  end.setHours(23, 59, 59);
 return start.setTime(end.getTime() - 3600 * 1000 * 24 * 7 + 1000);
}


function hepta(){
  const end = new Date();
  const start = new Date();
  end.setHours(23, 59, 59);
  return start.setTime(end.getTime() - 3600 * 1000 * 24 * 49 + 1000);
}


// 时间戳转时间段
function newdate(m) {
  
  
    var date2 = new Date(m);
    var Y2 = date2.getFullYear() + '';
    var M2 = (date2.getMonth() + 1 < 10 ? '0' + (date2.getMonth() + 1) : date2.getMonth() + 1) + '';
    var D2 = date2.getDate() < 10 ? '0' + date2.getDate() : date2.getDate();
    return (Y2 + M2 + D2);
  }
获取每月有几周

 // year:年  month:月  day:日
 getWeeks(year, month, day) {
    const d = new Date()
    // 该月第一天
    d.setFullYear(2018, 6, 1)
    let w1 = d.getDay()
    if (w1 === 0) {
      w1 = 7
    }
    // 该月天数
    d.setFullYear(2018, 7, 0)
    const dd = d.getDate()
    // 该月第一个周一
    let d1
    if (w1 !== 1) {
      d1 = 7 - w1 + 2
    } else {
      d1 = 1
    }
    const WEEK_NUB = Math.ceil((dd - d1 + 1) / 7)
    return WEEK_NUB
  }

获得周期名字

getWeekName() {
  const weekday = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  const index = new Date().getDay()
  const currDay = weekday[index]
  return currDay
}

获得当前日期在当月第几周

    // a: 年 b: 月  c: 日  (不包括跟上个月重合的部分)
    getMonthWeek(a, b, c) {
      const date = new Date(a, parseInt(b) - 1, c)
      const w = date.getDay()
      const d = date.getDate()
      return Math.ceil(
        (d + 6 - w) / 7
      )
    }
/**
 * 获取本周、本季度、本月、上月的开始日期、结束日期
 */
var now = new Date(); //当前日期
var nowDayOfWeek = now.getDay(); //今天本周的第几天
var nowDay = now.getDate(); //当前日
var nowMonth = now.getMonth(); //当前月
var nowYear = now.getYear(); //当前年
nowYear += (nowYear < 2000) ? 1900 : 0; //


var lastMonthDate = new Date(); //上月日期
lastMonthDate.setMonth(lastMonthDate.getMonth()-1);
var lastYear = lastMonthDate.getFullYear();
var lastMonth = lastMonthDate.getMonth();

var week_tag = 0;
var month_tag = 0;

//时间格式转换
function formatDate(value, fmt) {
    if (value == null || value == "") {
        return null;
    }
    var date = new Date(value);

    if(!fmt) {
        fmt = 'yyyy-MM-dd HH:mm:ss';
    }

    var o = {
        "M+": date.getMonth() + 1, //月份
        "d+": date.getDate(), //日
        "H+": date.getHours(), //小时
        "m+": date.getMinutes(), //分
        "s+": date.getSeconds(), //秒
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        "S": date.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.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;
}

//获得本周第一天
function getWeekStartDate() {
    var Nowdate=new Date();
    //今天是这周的第几天
    var today = Nowdate.getDay();
    // 如果今天是周日
    if (today == 0) {
        Nowdate=new Date(new Date().getTime()-24*60*60*1000)
    }
    var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
    M=Number(WeekFirstDay.getMonth())+1
    var d=WeekFirstDay.getDate();
    return formatDate(WeekFirstDay.getFullYear()+"-"+M+"-"+d, "yyyy-MM-dd");
}

//获得本周最后一天
function getWeekEndDate() {
    return formatDate(new Date(new Date(getWeekStartDate()).getTime()+6*24*60*60*1000), "yyyy-MM-dd");
}
//获得本月的开始日期
function getMonthStartDate() {
    var monthStartDate = new Date(nowYear, nowMonth, 1);
    return formatDate(monthStartDate, "yyyy-MM-dd");
}

//获得本月的结束日期
function getMonthEndDate(){
    var monthEndDate =getLastDay(nowYear, nowMonth);
    return formatDate(monthEndDate, "yyyy-MM-dd");
}

//获得上月开始时间
function getLastMonthStartDate(){
    var lastMonthStartDate = new Date(lastYear,lastMonth, 1);
    return formatDate(lastMonthStartDate, "yyyy-MM-dd");
}

//获得上月结束时间
function getLastMonthEndDate(){
    var lastMonthEndDate = getLastDay(lastYear, lastMonth); ;
    return formatDate(lastMonthEndDate, "yyyy-MM-dd");
}

// 获取指定年月的最后一天
function getLastDay(year,month) {
    var new_year=year ; //取当前的年份
    var new_month; ;//取下一个月的第一天,方便计算(最后一天不固定)
    if(month>=11) {
        new_month=0; //月份为1
        new_year++; //年份增 1
    } else{
        new_month=month+1;
    }
    var new_date = new Date(new_year,new_month,1); //取当年当月中的第一天
    return (new Date(new_date.getTime()-1000*60*60*24));//获取当月最后一天日期
}

function computeWeeks() {
    var dStartTime=new Date(getWeekStartDate());
    dStartTime.setDate(dStartTime.getDate() - week_tag * 7);

    var dEndTime=new Date(getWeekEndDate());
    dEndTime.setDate(dEndTime.getDate() - week_tag * 7);

    return {
        s: formatDate(dStartTime, "yyyy-MM-dd"),
        e: formatDate(dEndTime, "yyyy-MM-dd")
    }
}

// 上一周
function lastWeek() {
    week_tag++
    return computeWeeks()
}

// 下一周
function nextWeek() {
    week_tag--
    return computeWeeks()
}

function computeMonth() {
    var lastMonthDate = new Date(); //上月日期
    lastMonthDate.setMonth(lastMonthDate.getMonth() - month_tag);
    var lastYear = lastMonthDate.getFullYear();
    var lastMonth = lastMonthDate.getMonth();
    // 开始时间
    var monthStartDate = new Date(lastYear,lastMonth, 1);
    // 结束时间
    var monthEndDate = getLastDay(lastYear, lastMonth); ;

    return [formatDate(monthStartDate, "yyyy-MM-dd"), formatDate(monthEndDate, "yyyy-MM-dd")]
}

// 上一月
function getLastMonth() {
    month_tag++
    return computeMonth()
}

// 下一月
function getNextMonth() {
    month_tag--
    return computeMonth()
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值