Small micro-channel program date conversion, comparison, subtraction

Directly on dry goods:

In a new directory utils timeUtil.js, the code is as follows :( introduced js used where needed, the corresponding parameters can be passed in use)

//时间戳转日期
function toDate(number) {
  var n = number;
  var date = new Date(parseInt(n) * 1000);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  var h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  var minute = date.getMinutes();
  var second = date.getSeconds();
  minute = minute < 10 ? ('0' + minute) : minute;
  second = second < 10 ? ('0' + second) : second;
  return y + '-' + m + '-' + d + ' ' + h + ':' + minute+':' + second;
function mathChangeDate (date, method, days
Number of days // subtract the current date
}


  //method:'+' || '-'
  var timestamp = Date.parse(date);
  if(method == '+'){
    timestamp = timestamp / 1000 + 24 * 60 * 60 * days;
  } else if (method == '-'){
    timestamp = timestamp / 1000 - 24 * 60 * 60 * days;
  }
  return toDate(timestamp);
}

//时间戳转换具体时间
function getDateDiff(dateTimeStamp) {
  var result = '';
  var minute = 1000 * 60;
  var hour = minute * 60;
  var day = hour * 24;
  var halfamonth = day * 15;
  var month = day * 30;
  var now = new Date();//有些特殊 不能使用 new Date()
  var diffValue = now - dateTimeStamp;
  if (diffValue < 0) { return; }
  var monthC = diffValue / month;
  var weekC = diffValue / (7 * day);
  var dayC = diffValue / day;
  var hourC = diffValue / hour;
  var minC = diffValue / minute;
  if (monthC >= 1) {
    result = "" + parseInt(monthC) + "月前";
  }
  else if (weekC >= 1) {
    result = "" + parseInt(weekC) + "周前";
  }
  else if (dayC >= 1) {
    result = "" + parseInt(dayC) + "天前";
  }
  else if (hourC >= 1) {
    result = "" + parseInt(hourC) + "小时前";
  }
  else if (minC >= 1) {
    result = "" + parseInt(minC) + "分钟前";
  } else
    result = "刚刚";
// get the current time server
};
  Result return;


const formatDateThis = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return [year, month, day].map(formatNumber).join('-') +' '+ [hour, minute, second].map(formatNumber).join(':')
}

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('-')
}
const formatTimes = time => {
  const hour = time.getHours()
  time.getMinutes minute = const () 
  const = SECOND time.getSeconds () 
  return [hour, minute, SECOND] .map (formatNumber) .join ( ':') 
} 
// complement 0 
const = n-formatNumber => { 
  n-= n.toString () 
  ? n-return [. 1] n-: '0' + n- 
} 

// compare two times the size (refer to the format HH dd-mm-YYYY: mm: SS) 
function compareTime (the startTime, endTime) { 
  // ending time greater than the start time is true, otherwise it is to false 
  IF (startTime.localeCompare (endTime) == -1) { 
    return to true; 
  } 

  return to false; 
} 

module.exports = { 
  formatDateThis: formatDateThis, 
  FormatTime: FormatTime, 
  formatTimes: formatTimes , 
  toDate: toDate, 
  getDateDiff: getDateDiff, 
  mathChangeDate: mathChangeDate ,
  compareTime: compareTime
}

  

Guess you like

Origin www.cnblogs.com/nanyang520/p/11122492.html