Js—封裝時間轉換函數

因爲在實操中,後臺大多數都是直接給時間戳格式,故這裏的傳參都爲時間戳,(封裝方法爲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;
}

此博客會經常更新,主要用於分享,自己記錄,防止丟失等。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章