時間戳轉換爲具體時間(筆記)

function formatNumber (n) {
const str = n.toString()
return str[1] ? str : 0${str}
}

export function formatTime (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()

const t1 = [year, month, day].map(formatNumber).join('/')
const t2 = [hour, minute, second].map(formatNumber).join(':')

return `${t1} ${t2}`

}

//獲取現在時間
export function getNowTime(){
let myDate = new Date();
let hours = myDate.getHours(); //獲取當前小時數(0-23)
let minutes = myDate.getMinutes(); //獲取當前分鐘數(0-59)
let seconds = myDate.getSeconds(); //獲取當前秒數(0-59)
return formatNumber(hours) + ‘:’ + formatNumber(minutes) + ‘:’ + formatNumber(seconds);
}

//獲取現在日期前後10年
export function getNowDate(fmt){
let myDate = new Date();
let myMonth = formatNumber(myDate.getMonth() + 1);
let mydate = formatNumber(myDate.getDate());
let date = {
pickerStart: myDate.getFullYear(),
startDate: (myDate.getFullYear()-10),
endDate: (myDate.getFullYear()+10)
}
if(fmt && fmt.indexOf(‘YY:MM’) > -1){
date = {
pickerStart: myDate.getFullYear() + ‘-’ + myMonth,
startDate: (myDate.getFullYear()-10) + ‘-’ + myMonth,
endDate: (myDate.getFullYear()+10) + ‘-’ + myMonth
}
}else if(fmt && fmt.indexOf(‘YY:MM:DD’) > -1){
date = {
pickerStart: myDate.getFullYear() + ‘-’ + myMonth + ‘-’ + mydate,
startDate: (myDate.getFullYear()-10) + ‘-’ + myMonth + ‘-’ + mydate,
endDate: (myDate.getFullYear()+10) + ‘-’ + myMonth + ‘-’ + mydate
}
}
return date
}

//時間戳轉換時間
export function timestampFormat(timestamp, fmt, type){
fmt = fmt || “YY:MM:DD hh:mm:ss”;
timestamp = typeof(timestamp) == ‘string’ ? parseInt(timestamp) : timestamp;
var time = new Date(timestamp);
var year = time.getFullYear();
var month = time.getMonth()+1;
var day = time.getDate();
var hours = time.getHours();
var minutes = time.getMinutes();
var seconds = time.getSeconds();

var outTime = "";
if(fmt.indexOf("YY") > -1){
  if(type && type == 'ch'){
    outTime += '-' + year + '年';
  }else{
    outTime += '-' + year;
  }
}
if(fmt.indexOf("MM") > -1){
  if(type && type == 'ch'){
    outTime += formatNumber(month) + '月';
  }else{
    outTime += '-' + formatNumber(month);
  }
}
if(fmt.indexOf("DD") > -1){
  if(type && type == 'ch'){
    outTime += formatNumber(day) + '日';
  }else{
    outTime += '-' + formatNumber(day);
  }
}
if(fmt.indexOf("hh") > -1){
  if(type && type == 'ch'){
    outTime += ' '+formatNumber(hours) + '小時';
  }else{
    outTime += ' '+formatNumber(hours);
  }
}
if(fmt.indexOf("mm") > -1){
  if(type && type == 'ch'){
    outTime += formatNumber(minutes) + '分';
  } else {
    outTime += ':'+formatNumber(minutes);
  }
}
if(fmt.indexOf("ss") > -1){
  if(type && type == 'ch'){
    outTime += formatNumber(seconds) + '秒';
  } else {
    outTime += ':'+formatNumber(seconds);
  }
}
return outTime.slice(1);

}

//時間戳轉換星期
export function weekFormat(timestamp){
let date = new Date(timestamp);
let week = ‘’
switch (date.getDay()) {
case 0: week = “星期天”; break
case 1: week = “星期一”; break
case 2: week = “星期二”; break
case 3: week = “星期三”; break
case 4: week = “星期四”; break
case 5: week = “星期五”; break
case 6: week = “星期六”; break
}
return week;
}

export default {
formatNumber
}

// var qrcode = new QRCode(elem, {
// width : 96,//設置寬高
// height : 96
// });
// qrcode.makeCode(“http://www.baidu.com”);

使用方法:
引入文件中的方法:
import { timestampFormat } from ‘/uitils’
this.class_time = timestampFormat(
this.class_time * 1000,
“YY:MM:DD”
);
//轉換到日期
this.class_time = timestampFormat(
this.class_time * 1000,
“YY:MM:DD HH:mm:ss”
);
//轉換到時分秒

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