欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Js—封装时间转换函数

程序员文章站 2024-01-28 20:00:04
...

因为在实操中,后台大多数都是直接给时间戳格式,故这里的传参都为时间戳,(封装方法为ES6,适用于微信小程序,vue 等框架,也适用于正常js)

注:时间戳为10位需*1000,时间戳为13位的话不需乘1000

普通时间戳转化年月日

const timeStamp = dateStamp => { // 转化格式为2019年2月28日,可自己修改
  let date = new Date(dateStamp);
  let Y = date.getFullYear() + '年';
  let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '月';
  let D = date.getDate() + '日';
  let tempDate = Y + M + D;
  return tempDate;
}

转化为年月日周(大写小写周)

const timeYMDWeek = dateStamp => { // 转化为年月日周--------
  let date = new Date(dateStamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
  let Y = date.getFullYear(); // 年
  let M = date.getMonth() + 1; //  月  
  let D = date.getDate(); // 日
  let W = date.getDay(); // 周 0-6(周日到周六)
  let Ww = date.getDay(); // 周日到周六
  //使用数组更改日期样式;
  var dayCycleArray = ["日", "一", "二", "三", "四", "五", "六"];
  for (let i = 0; i < 7; i++) {
    if (W == i) {
      //将dayCycleArray的数赋值到系统星期几里面中去;
      Ww = dayCycleArray[i];
    }
  }
  let selfTempDate = [Y, M, D, W, Ww];
  return selfTempDate;
}

注意周是从周日开始到周六 (即0-6,0代表周日,6代表周六类推)

时间戳转换今天昨天

const timeSetTodayAndYesterday = dateStamp => { // 转化为今天昨天
  // 如果年月日和服务器时间相等 转化 否则02-21
  // 1.接收传入时间
  let tt = new Date(dateStamp);
  let days = parseInt((new Date().getTime() - tt) / 86400000);
  // 今天时间
  var today = new Date().getDate();

  var year = tt.getFullYear();
  var mouth = tt.getMonth() + 1 < 10 ? '0' + (tt.getMonth() + 1) : tt.getMonth() + 1;
  var day = tt.getDate() < 10 ? '0' + tt.getDate() : tt.getDate();
  var time = tt.getHours() < 10 ? "0" + tt.getHours() : tt.getHours();
  var min = tt.getMinutes() < 10 ? "0" + tt.getMinutes() : tt.getMinutes();
  var result, offset;       
  offset = Math.abs(today - day);
  if (days < 2 && offset < 2) {
    if (offset === 0) {
      result = "今天";
    }
    if (offset === 1) {
      result = "昨天";
    }
    //  else if (offset === 2) {
    // result = "前天" + time + ":" + min;
  } else {
    result = mouth + "-" + day
  }
  var todayAndYesterday = [result, time, min];
  return todayAndYesterday;
}

判断条件days 可改为不同值来判断天数,还可加至多少分钟前等

判断两时间戳是否相等

const timeIsToday = dateStamp => { // 判断两时间戳是否相等
  let dateTime = new Date(dateStamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
  let Y = dateTime.getFullYear();
  let M = dateTime.getMonth();
  let D = dateTime.getDate();
  //  取当前时间
  let today = new Date();
  let today_Y = today.getFullYear();
  let today_M = today.getMonth();
  let today_D = today.getDate();
  if (today_Y == Y && today_M == M && today_D == D) {
    return true;
  }
  return false;
}

阿拉伯数字转换为简写汉字

const arabia_To_SimplifiedChinese = (num) => {
  for (let i = num.length - 1; i >= 0; i--) {
    num = num.replace(",", "") //替换num中的“,”
    num = num.replace(" ", "") //替换num中的空格
  }
  if (isNaN(num)) { //验证输入的字符是否为数字
    //alert("请检查小写金额是否正确");
    return;
  }
  var part;
  var newchar;
  //字符处理完毕后开始转换,采用前后两部分分别转换
  part = String(num).split(".");
  newchar = "";
  //小数点前进行转化
  for (let i = part[0].length - 1; i >= 0; i--) {
    if (part[0].length > 10) {
      //alert("位数过大,无法计算");
      return "";
    } //若数量超过拾亿单位,提示
    var tmpnewchar;
    var perchar;
    tmpnewchar = ""
    perchar = part[0].charAt(i);
    switch (perchar) {
      case "0":
        tmpnewchar = "零" + tmpnewchar;
        break;
      case "1":
        tmpnewchar = "一" + tmpnewchar;
        break;
      case "2":
        tmpnewchar = "二" + tmpnewchar;
        break;
      case "3":
        tmpnewchar = "三" + tmpnewchar;
        break;
      case "4":
        tmpnewchar = "四" + tmpnewchar;
        break;
      case "5":
        tmpnewchar = "五" + tmpnewchar;
        break;
      case "6":
        tmpnewchar = "六" + tmpnewchar;
        break;
      case "7":
        tmpnewchar = "七" + tmpnewchar;
        break;
      case "8":
        tmpnewchar = "八" + tmpnewchar;
        break;
      case "9":
        tmpnewchar = "九" + tmpnewchar;
        break;
    }
    switch (part[0].length - i - 1) {
      case 0:
        tmpnewchar = tmpnewchar;
        break;
      case 1:
        if (perchar != 0) tmpnewchar = tmpnewchar + "十";
        break;
      case 2:
        if (perchar != 0) tmpnewchar = tmpnewchar + "百";
        break;
      case 3:
        if (perchar != 0) tmpnewchar = tmpnewchar + "千";
        break;
      case 4:
        tmpnewchar = tmpnewchar + "万";
        break;
      case 5:
        if (perchar != 0) tmpnewchar = tmpnewchar + "十";
        break;
      case 6:
        if (perchar != 0) tmpnewchar = tmpnewchar + "百";
        break;
      case 7:
        if (perchar != 0) tmpnewchar = tmpnewchar + "千";
        break;
      case 8:
        tmpnewchar = tmpnewchar + "亿";
        break;
      case 9:
        tmpnewchar = tmpnewchar + "十";
        break;
    }
    newchar = tmpnewchar + newchar;
  }
  //替换所有无用汉字,直到没有此类无用的数字为止
  while (newchar.search("零零") != -1 || newchar.search("零亿") != -1 || newchar.search("亿万") != -1 || newchar.search("零万") != -1) {
    newchar = newchar.replace("零亿", "亿");
    newchar = newchar.replace("亿万", "亿");
    newchar = newchar.replace("零万", "万");
    newchar = newchar.replace("零零", "零");
  }
  //替换以“一十”开头的,为“十”
  if (newchar.indexOf("一十") == 0) {
    newchar = newchar.substr(1);
  }
  //替换以“零”结尾的,为“”
  if (newchar.lastIndexOf("零") == newchar.length - 1) {
    newchar = newchar.substr(0, newchar.length - 1);
  }
  return newchar;
}

此博客会经常更新,主要用于分享,自己记录,防止丢失等。。。