1糙箍、秒轉(zhuǎn)換成 時(shí):分:秒
export const formatSeconds = (value:number)=>{
var secondTime = parseInt(value);// 秒
var minuteTime = 0;// 分
var hourTime = 0;// 小時(shí)
if(secondTime > 60) {//如果秒數(shù)大于60,將秒數(shù)轉(zhuǎn)換成整數(shù)
//獲取分鐘牵祟,除以60取整數(shù)深夯,得到整數(shù)分鐘
minuteTime = parseInt(secondTime / 60);
//獲取秒數(shù),秒數(shù)取佘诺苹,得到整數(shù)秒數(shù)
secondTime = parseInt(secondTime % 60);
//如果分鐘大于60咕晋,將分鐘轉(zhuǎn)換成小時(shí)
if(minuteTime > 60) {
//獲取小時(shí),獲取分鐘除以60收奔,得到整數(shù)小時(shí)
hourTime = parseInt(minuteTime / 60);
//獲取小時(shí)后取佘的分掌呜,獲取分鐘除以60取佘的分
minuteTime = parseInt(minuteTime % 60);
}
}else{
return "00:" + secondTime
}
var result = "" + parseInt(secondTime) ;
if(minuteTime > 0) {
result = "" + parseInt(minuteTime) + ":" + result;
}
if(hourTime > 0) {
result = "" + parseInt(hourTime) + ":" + result;
}
return result;
}
2、時(shí)間戳轉(zhuǎn)為 幾天(小時(shí)坪哄、分鐘)前质蕉、剛剛
export const timeFormat = (time:number| string) => {
let one_minutes = 60 //1分鐘 60秒
let one_hours = 3600 //1小時(shí) 3600秒
let one_day = 86400 //1天 86400秒
let cur_date = new Date()
let old_date = new Date(time)
let cur_years = cur_date.getFullYear()//現(xiàn)在的年份
let old_years = old_date.getFullYear()//比較的年份
let new_datetime = cur_date.getTime(); //現(xiàn)在的時(shí)間
let old_datetime = old_date.getTime(); //過去比較的一個(gè)時(shí)間點(diǎn)
let difftime = Math.floor((new_datetime - old_datetime)/1000) //相差秒數(shù)
let minutes = Math.floor(difftime/one_minutes); // 相差分鐘 60秒
let hours = Math.floor(difftime/one_hours); // 相差小時(shí) 60*60 秒
let days = Math.floor(difftime/one_day); // 相差天 24*60*60 秒
let timeTxt = ''
if(difftime<60){
//1分鐘內(nèi)
timeTxt = G.LANG.time_just_now
}
if(difftime>=60 && difftime< one_hours){
//60分鐘內(nèi)
timeTxt = mo.STR.placeholder(G.LANG.time_minute_ago,{num:minutes})
}
if(difftime>=one_hours && difftime<one_hours*24){
//24小時(shí)內(nèi)
timeTxt = mo.STR.placeholder(G.LANG.time_hour_ageo,{num:hours})
}
if(difftime>=one_hours*24 && difftime<one_hours*24*3){
//24-48小時(shí)內(nèi) 48-72小時(shí)內(nèi)
timeTxt = mo.STR.placeholder(G.LANG.time_days_ago,{num:days})
}
// if(difftime>=3600*24*2 && difftime<3600*24*2){
// //48-72小時(shí)內(nèi)
// timeTxt = '2天前'
// }
if(difftime>=one_hours*24*3){
//48-72小時(shí)內(nèi)
let showYear = cur_years != old_years
timeTxt = timestampToDay(time,showYear)
}
return timeTxt
}
/**
* 動(dòng)態(tài)時(shí)間
*/
function timestampToDay(timestamp:number| string,showYear:boolean=true) {
var date = new Date(timestamp); //時(shí)間戳為10位需*1000,時(shí)間戳為13位的話不需乘1000
var Y = date.getFullYear() + '-';
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
return `${showYear?Y:''}${M}${D}`
// return Y + M + D ;
}
3翩肌、日期轉(zhuǎn)周
getWeekStr(date){
let weekNum = date.getDay()
if(weekNum === 0){
return '周日'
}
if(weekNum === 1){
return '周一'
}
if(weekNum === 2){
return '周二'
}
if(weekNum === 3){
return '周三'
}
if(weekNum === 4){
return '周四'
}
if(weekNum === 5){
return '周五'
}
if(weekNum === 6){
return '周六'
}
}
4饰剥、date轉(zhuǎn)指定格式,不如 MM-dd
formatDate(date,fmt){
var o = {
"M+": date.getMonth() + 1, //月份
"d+": date.getDate(), //日
"h+": date.getHours(), //小時(shí)
"m+": date.getMinutes(), //分
"s+": date.getSeconds(), //秒
"q+": Math.floor((date.getMonth() + 3) / 3), //季度
"S": date.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
5、使用moment獲取時(shí)間
import moment from "moment/moment";
export const toYear = (moment(new Date()).format('YYYY'))
export const toMonth = (moment(new Date()).format('MM'))
export const toDay = (moment(new Date()).format('DD'))
6摧阅、獲取當(dāng)月的天數(shù)
//獲取當(dāng)月的天數(shù)
export const getDaysOfMonth = (year, month) => {
var day = new Date(year, month, 0)
var dayCount = day.getDate()
return dayCount
}
7汰蓉、獲取幾天前后的日期
// 2.獲取幾天之前或幾天之后的日期 傳入date和天數(shù)
getBeforeDayDate(date,day) {
let timestamp = date.getTime();
// 獲取day天前的日期
return new Date(timestamp - day * 24 * 3600 * 1000);
}
getAfterDayDate(date,day) {
let timestamp = date.getTime();
// 獲取day天之后的日期
return new Date(timestamp + day * 24 * 3600 * 1000);
}