1、秒转换成 时:分:秒
export const formatSeconds = (value:number)=>{
var secondTime = parseInt(value);// 秒
var minuteTime = 0;// 分
var hourTime = 0;// 小时
if(secondTime > 60) {//如果秒数大于60,将秒数转换成整数
//获取分钟,除以60取整数,得到整数分钟
minuteTime = parseInt(secondTime / 60);
//获取秒数,秒数取佘,得到整数秒数
secondTime = parseInt(secondTime % 60);
//如果分钟大于60,将分钟转换成小时
if(minuteTime > 60) {
//获取小时,获取分钟除以60,得到整数小时
hourTime = parseInt(minuteTime / 60);
//获取小时后取佘的分,获取分钟除以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、时间戳转为 几天(小时、分钟)前、刚刚
export const timeFormat = (time:number| string) => {
let one_minutes = 60 //1分钟 60秒
let one_hours = 3600 //1小时 3600秒
let one_day = 86400 //1天 86400秒
let cur_date = new Date()
let old_date = new Date(time)
let cur_years = cur_date.getFullYear()//现在的年份
let old_years = old_date.getFullYear()//比较的年份
let new_datetime = cur_date.getTime(); //现在的时间
let old_datetime = old_date.getTime(); //过去比较的一个时间点
let difftime = Math.floor((new_datetime - old_datetime)/1000) //相差秒数
let minutes = Math.floor(difftime/one_minutes); // 相差分钟 60秒
let hours = Math.floor(difftime/one_hours); // 相差小时 60*60 秒
let days = Math.floor(difftime/one_day); // 相差天 24*60*60 秒
let timeTxt = ''
if(difftime<60){
//1分钟内
timeTxt = G.LANG.time_just_now
}
if(difftime>=60 && difftime< one_hours){
//60分钟内
timeTxt = mo.STR.placeholder(G.LANG.time_minute_ago,{num:minutes})
}
if(difftime>=one_hours && difftime<one_hours*24){
//24小时内
timeTxt = mo.STR.placeholder(G.LANG.time_hour_ageo,{num:hours})
}
if(difftime>=one_hours*24 && difftime<one_hours*24*3){
//24-48小时内 48-72小时内
timeTxt = mo.STR.placeholder(G.LANG.time_days_ago,{num:days})
}
// if(difftime>=3600*24*2 && difftime<3600*24*2){
// //48-72小时内
// timeTxt = '2天前'
// }
if(difftime>=one_hours*24*3){
//48-72小时内
let showYear = cur_years != old_years
timeTxt = timestampToDay(time,showYear)
}
return timeTxt
}
/**
* 动态时间
*/
function timestampToDay(timestamp:number| string,showYear:boolean=true) {
var date = new Date(timestamp); //时间戳为10位需*1000,时间戳为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、日期转周
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转指定格式,不如 MM-dd
formatDate(date,fmt){
var o = {
"M+": date.getMonth() + 1, //月份
"d+": date.getDate(), //日
"h+": date.getHours(), //小时
"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获取时间
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、获取当月的天数
//获取当月的天数
export const getDaysOfMonth = (year, month) => {
var day = new Date(year, month, 0)
var dayCount = day.getDate()
return dayCount
}
7、获取几天前后的日期
// 2.获取几天之前或几天之后的日期 传入date和天数
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);
}