小程序js格式化时间

js处理格式化时间

 

/* 获取当前时间或格式化时间戳或获取时间戳
date为时间,不传为当前时间;
ostyle为时间风格:'-'或'/'或'zh',不传默认为'-' */
const getFormatDate = (str, ostyle) => {
  var oDate = new Date();
  if (!isEmpty(str)) {
    if (typeof str === "string") {
      if (str.indexOf("T") > -1) {
        str = getdisposeTime(str); // 2019-04-07T16:00:00.000+0000格式
      } else if (str.indexOf(".") > -1) {
        str = str.substring(0, str.indexOf(".")); // 有些日期接口返回带有.0。
      } else if (isDatetime(str) && str.length > 10) { //2021-01-01 12:00:00
        // str += ' 00:00:00';
      } else if (strDate(str) && (str.length > 8 && str.length <= 10)) { //2021-01-01
        str += ' 00:00:00';
      } else if (isTime(str) && (str.length <= 8)) { //12:00:00
        var strYear = oDate.getFullYear(),
          strMonth = oDate.getMonth() + 1,
          strDay = oDate.getDate();
        str = strYear + '-' + formatNumber(strMonth) + '-' + formatNumber(strDay) + ' ' + str;
      }
      // 解决IOS上无法从str parse 到Date类型问题
      str = str.replace(/-/g, '/');
    }
    oDate = new Date(str);
  }
  const oYear = oDate.getFullYear();
  const oMonth = oDate.getMonth() + 1;
  const oDay = oDate.getDate();
  const oHour = oDate.getHours();
  const oMin = oDate.getMinutes();
  const oSen = oDate.getSeconds();
  return getDataTimeInfo(oDate, oYear, oMonth, oDay, oHour, oMin, oSen, ostyle);
}
/*  返回时间格式数据,function(年,月,日,时,分,秒,风格){}
         *   风格style,值为:'-'或'/'或'zh',不传默认为'-'
         *   返回值:obj = {
         oYmdStr:年月日,
         oYmdHms:年月日时分秒,
         oDateTime: 年-月-日  时:分:秒,
         nowToday: 年-月-日 00:00:00,
         nowTodays: 年-月-日  23:59:59,
         oDateHms: 年-月-日  时:分:00,
         oDateHm: 年-月-日  时:分,
         oDate: 年-月-日,
         oTime: 时:分:秒,
         oHm: 时:分,
         xtstNowToday:,当天23:59时时间戳
         xtstNowNextDay:,当天0點时时间戳
         xtstNowDate:,當前時間戳
         xtstTime:,需要格式化时间戳的时间(有问题)
         }*/
const getDataTimeInfo = (str, year, month, day, h, m, s, style) => {
  var ostyle = '-',
    ostyle1 = '-',
    ostyle2 = '';
  if (style == '/') {
    ostyle = '/';
    ostyle1 = '/';
    ostyle2 = '';
  } else if (style == 'zh') {
    ostyle = '年';
    ostyle1 = '月';
    ostyle2 = '日';
  }
  var obj = {
    oYmdStr: `${[year, month, day].map(formatNumber).join('')}`,
    oYmdHms: `${[year, month, day].map(formatNumber).join('')}${[h, m, s].map(formatNumber).join('')}`,
    oDateTime: `${[year, month, day].map(formatNumber).join(ostyle)} ${[h, m, s].map(formatNumber).join(':')}`,
    nowToday: `${[year, month, day].map(formatNumber).join(ostyle)} 00:00:00`,
    nowTodays: `${[year, month, day].map(formatNumber).join(ostyle)} 23:59:59`,
    oDateHms: `${[year, month, day].map(formatNumber).join(ostyle)} ${[h, m].map(formatNumber).join(':')}:00`,
    oDateHm: `${[year, month, day].map(formatNumber).join(ostyle)} ${[h, m].map(formatNumber).join(':')}`,
    oDate: `${[year, month, day].map(formatNumber).join(ostyle)}`,
    oTime: `${[h, m, s].map(formatNumber).join(':')}`,
    oHm: `${[h, m].map(formatNumber).join(':')}`,
    xtstNowToday: (new Date(new Date(new Date().getTime() - 24 * 60 * 60 * 1000).setHours(23, 59, 59, 999))).getTime(),
    xtstNowNextDay: (new Date(new Date(new Date().getTime() - 24 * 60 * 60 * 1000).setHours(0, 0, 0, 0))).getTime(),
    xtstNowDate: (new Date()).valueOf(),
    xtstTime: new Date(str).getTime()
  }
  return obj;
}
// 为个位数时补0
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}
/**
 * @param date 传递过来的时间字符串 特殊处理格式 2019-04-07T16:00:00.000+0000
 */
const getdisposeTime = date => {
  var arr = date.split("T");
  var d = arr[0];
  var darr = d.split('-');
  var t = arr[1];
  var tarr = t.split('.000');
  var marr = tarr[0].split(':');
  var dd = parseInt(darr[0]) + "/" + parseInt(darr[1]) + "/" + parseInt(darr[2]) + " " + parseInt(marr[0]) + ":" + parseInt(marr[1]) + ":" + parseInt(marr[2]);
  return dd;
}
/**
 * 判断日期格式 2021-01-01 12:00:00
 * @param str  时间
 * @returns Boolean
 */
const isDatetime = date => {
  var regex = /^(?:19|20)[0-9][0-9]-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:[0-2][1-9])|(?:[1-3][0-1])) (?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]:[0-5][0-9]$/;
  if (!regex.test(date)) {
    // alert("格式不正确!请输入正确的时间格式,如:2010-07-07 09:12:00");
    return false;
  }
  // alert("格式正确!");
  return true;
}
/**
 * 判断短时间格式 12:00:00
 * @param str  时间
 * @returns Boolean
 */
const isTime = str => {
  var a = str.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/);
  if (a == null) {
    // alert('输入的参数不是时间格式');
    return false;
  }
  if (a[1] > 24 || a[3] > 60 || a[4] > 60) {
    // alert("时间格式不对");
    return false
  }
  return true;
}
/**
 * 判断短日期格式 2021-01-01
 * @param str  时间
 * @returns Boolean
 */
const strDate = str => {
  var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
  if (r == null) return false;
  var d = new Date(r[1], r[3] - 1, r[4]);
  return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]);
}

 

posted @ 2022-04-25 15:14  时光独醒  阅读(33)  评论(0编辑  收藏  举报