utils->js->ecma->date(工具)



/**
 * 时间戳转化成时间
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string | null}
 */
export function parseTime(time, cFormat) {
    if (arguments.length === 0 || !time) {
      return null
    }
    const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
    let date
    if (typeof time === 'object') {
      date = time
    } else {
      if ((typeof time === 'string')) {
        if ((/^[0-9]+$/.test(time))) {
          // support "1548221490638"
          time = parseInt(time)
        } else {
          // support safari
          // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
          time = time.replace(new RegExp(/-/gm), '/')
        }
      }
  
      if ((typeof time === 'number') && (time.toString().length === 10)) {
        time = time * 1000
      }
      date = new Date(time)
    }
    const formatObj = {
      y: date.getFullYear(),
      m: date.getMonth() + 1,
      d: date.getDate(),
      h: date.getHours(),
      i: date.getMinutes(),
      s: date.getSeconds(),
      a: date.getDay()
    }
    const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
      const value = formatObj[key]
      // Note: getDay() returns 0 on Sunday
      if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
      return value.toString().padStart(2, '0')
    })
    return time_str
  }
  
  /**
   * 
   *  _this.realTime = formatStamp(
        new Date().getTime(),
        "YYYY-MM-DD hh:mm",
        true
      );
   * 时间戳转化成时间
   * @export
   * @param  {any} arg 
   * @return 
   */
  export function  formatStamp(t,str,switchZero) {
    switchZero=typeof(switchZero)!='undefined'?switchZero:true;
    var date = new Date(t),//时间戳为10位需*1000,时间戳为13位的话不需乘1000
        Y = date.getFullYear() ,
        M = (date.getMonth()+1 < 10 &&switchZero? '0'+(date.getMonth()+1) : date.getMonth()+1),
        D = (date.getDate()< 10 &&switchZero? '0'+(date.getDate()) : date.getDate()) ,
        h = (date.getHours()< 10 &&switchZero? '0'+(date.getHours()) : date.getHours()) ,
        m = (date.getMinutes()< 10 &&switchZero? '0'+(date.getMinutes()) : date.getMinutes()) , 
        s = (date.getSeconds()< 10 &&switchZero? '0'+(date.getSeconds()) : date.getSeconds()) ,
    str=str.replace('YYYY',Y).replace('MM',M).replace('DD',D).replace('hh',h).replace('mm',m).replace('ss',s);
    return str;
  }

  
/**
 *计算经过了多少时间
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatTime(time, option) {
    if (('' + time).length === 10) {
      time = parseInt(time) * 1000
    } else {
      time = +time
    }
    const d = new Date(time)
    const now = Date.now()
  
    const diff = (now - d) / 1000
  
    if (diff < 30) {
      return '刚刚'
    } else if (diff < 3600) {
      // less 1 hour
      return Math.ceil(diff / 60) + '分钟前'
    } else if (diff < 3600 * 24) {
      return Math.ceil(diff / 3600) + '小时前'
    } else if (diff < 3600 * 24 * 2) {
      return '1天前'
    }
    if (option) {
      return parseTime(time, option)
    } else {
      return (
        d.getMonth() +
        1 +
        '月' +
        d.getDate() +
        '日' +
        d.getHours() +
        '时' +
        d.getMinutes() +
        '分'
      )
    }
  }
  
/*
获得连续的年份
param:num,几个连续的年份,minyear最小年份(非必须,可能使年份总数减少),addyear增加的年份上限(非必须)
return:连续的年份,数组
auth:yc
time:19/11/25
*/
export function consecutiveYear(num, minyear, addyear) {
  var yearArr = [],
      nowYaer = Number((new Date()).getFullYear()) + (addyear ? addyear : 0),
      minyear = minyear ? Number(minyear) : null;

  for (var i = 0; i < num; i++) {
      if (minyear) {
          if (nowYaer >= minyear) {
              yearArr.unshift(nowYaer--)
          };
      } else {
          yearArr.unshift(nowYaer--)
      };
  };
  return yearArr;
};

/*
根据时间计算周几
入参:字符串,1994-10-16
返参:星期X
auth: 杨超
time:2019/10/09
*/ 
export function calculationDay(str){
  var arys=str.split('-');
  var ssdate = new Date(arys[0], parseInt(arys[1] - 1), arys[2]);
  var weekNum=String(ssdate.getDay()).replace("0","日").replace("1","一").replace("2","二").replace("3","三").replace("4","四").replace("5","五").replace("6","六")//就是你要的星期几
  var weekX="星期"+weekNum; //就是你要的星期几
  return weekX;
};

/*
检查日期格式
入参:
返参:无
auth:
time:
*/
function CheckCalcDate(n) {
  var t = $("#BeginDate").val(),
    i = $("#EndDate").val();
  t = t == "" ? "19000101" : t.replace("-", "") + "01";
  i = i == "" ? "20991231" : i.replace("-", "") + "31";
  var r = n.split("-"),
    u = r[0].replace(".", "").replace(".", ""),
    f = r[1].replace(".", "").replace(".", "");
  return parseInt(u, 10) >= parseInt(t, 10) &&
    parseInt(f, 10) <= parseInt(i, 10)
    ? !0
    : !1;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值