【JS】JS格式化时间

2021-05-31 17:03

阅读:590

标签:cal   str   gettime   date()   ==   real   local   问题   code   

// 判斷是否已登錄授權
module.exports.getDate = getDate
function getDate(){
  var time = new Date().toLocaleDateString().split(‘/‘).join(‘-‘)
  return time;
}

// 判斷是否已登錄授權
module.exports.getDayTimeStamp = getDayTimeStamp
function getDayTimeStamp(day){
  var time = getDay(day)//new Date().toLocaleDateString().split(‘/‘).join(‘-‘)
  return Date.parse(new Date(time)) - 1000*60*60*24;
}

//获取 日期, day = 0 为当天日期,-1,为前一天日期,1,为明天日期
module.exports.getDay = getDay
function getDay(day){
  var today = new Date();
  var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;
  today.setTime(targetday_milliseconds); //注意,这行是关键代码
  var tYear = today.getFullYear();
  var tMonth = today.getMonth();
  var tDate = today.getDate();
  tMonth = doHandleMonth(tMonth + 1);
  tDate = doHandleMonth(tDate);
  return tYear+"-"+tMonth+"-"+tDate;
}

function doHandleMonth(month){
  var m = month;
  if(month.toString().length == 1){
    m = "0" + month;
  }
  return m;
}

// 时间戳 转为 日期 年月日
module.exports.timeStamp2Date = timeStamp2Date
function timeStamp2Date(timeStamp){
  let date = new Date(timeStamp)
  let Y = date.getFullYear() + ‘-‘;
  let M = (date.getMonth()+1 ;
  let D = date.getDate();
  return Y+M+D
}
// 时间戳 转为 时间 年月日时分秒
module.exports.timeStamp2DateTime = timeStamp2DateTime
function timeStamp2DateTime(timeStamp){
  let date = new Date(timeStamp)
  let Y = date.getFullYear() + ‘-‘;
  let M = (date.getMonth()+1 ;
  let D = date.getDate() + ‘ ‘;
  let h = date.getHours() + ‘:‘;
  let m = date.getMinutes() + ‘:‘;
  let s = date.getSeconds(); 
  return Y+M+D+h+m+s
}

// 秒钟转成时分秒      输出03:05:59  时分秒
module.exports.secondToDate = secondToDate
function secondToDate(result) {
  var h = Math.floor(result / 3600) );
  var m = Math.floor((result / 60 % 60)) ));
  var s = Math.floor((result % 60)) ));
  return result = h + ":" + m + ":" + s;
}

//日期格式化-------------------------------------------------------------
module.exports.formatTime = formatTime
var formatTime = function (strDate, format = "yyyy-MM-dd hh:mm:ss") {
  // 解决ios出现NaN问题
  var realDate = strDate ? getDate(strDate.replace(getRegExp(‘-‘, ‘g‘), ‘/‘)) : getDate();
  var regYear = getRegExp("(y+)", "i");
  var date = [
    ["M+", realDate.getMonth() + 1],
    ["d+", realDate.getDate()],
    ["h+", realDate.getHours()],
    ["m+", realDate.getMinutes()],
    ["s+", realDate.getSeconds()],
    ["q+", Math.floor((realDate.getMonth() + 3) / 3)],
    ["S+", realDate.getMilliseconds()],
  ];
  var reg1 = regYear.exec(format);
  if (reg1) {
    format = format.replace(reg1[1], (realDate.getFullYear() + ‘‘).substring(4 - reg1[1].length));
  }
  for (var i = 0; i ) {
    var reg2 = getRegExp("(" + date[i][0] + ")").exec(format);
    if (reg2) {
      format = format.replace(reg2[1], reg2[1].length == 1 ? v : ("00" + date[i][1]).substring(("" + date[i][1]).length));
    }
  }
  return format;
}

 

【JS】JS格式化时间

标签:cal   str   gettime   date()   ==   real   local   问题   code   

原文地址:https://www.cnblogs.com/cnchg/p/14616194.html


评论


亲,登录后才可以留言!