Files
learning-system-portal/src/utils/datetime.js
2022-06-07 16:30:28 +08:00

113 lines
2.8 KiB
JavaScript

// export function formatsec (date, fmt) {
// if (/(y+)/.test(fmt)) {
// fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
// }
// let o = {
// 'M+': new date(date).getMonth() +1,
// 'd+': date.getDate(),
// 'h+': date.getHours(),
// 'm+': date.getMinutes(),
// 's+': date.getSeconds()
// };
// for (let k in o) {
// if (new RegExp(`(${k})`).test(fmt)) {
// let str = o[k] + '';
// fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
// }
// }
// return fmt;
// };
// function padLeftZero (str) {
// return ('00' + str).substr(str.length);
// }
export function formatsec(date){
var year = new Date(date).getFullYear(),
month = new Date(date).getMonth() + 1,//月份是从0开始的
day = new Date(date).getDate(),
hour = new Date(date).getHours(),
min = new Date(date).getMinutes(),
sec = new Date(date).getSeconds();
if(month<10){month='0'+month}
if(day<10){day='0'+day}
if(hour<10){hour='0'+hour}
if(min<10){min='0'+min}
if(sec<10){sec='0'+sec}
var newTime = year + '-' +
month + '-' +
day + ' ' +
hour + ':' +
min + ':' +
sec;
return newTime;
}
export function formatDate(date){
var year = date.getFullYear(),
month = date.getMonth() + 1,//月份是从0开始的
day = date.getDate(),
hour = date.getHours(),
min = date.getMinutes(),
sec = date.getSeconds();
if(month<10){month='0'+month}
if(day<10){day='0'+day}
if(hour<10){hour='0'+hour}
if(min<10){min='0'+min}
if(sec<10){sec='0'+sec}
var newTime = year + '-' +
month + '-' +
day + ' ' +
hour + ':' +
min + ':' +
sec;
return newTime;
}
// 秒转时分秒
export function formatSeconds(value) {
let result = parseInt(value)
let d= Math.floor(result / (3600*24));
let last=Math.floor(result % (3600*24));
let h = Math.floor(last / 3600) < 10 ? '0' + Math.floor(last / 3600) : Math.floor(last / 3600);
let m = Math.floor((last / 60 % 60)) < 10 ? '0' + Math.floor((last / 60 % 60)) : Math.floor((last / 60 % 60));
let s = Math.floor((last % 60)) < 10 ? '0' + Math.floor((last % 60)) : Math.floor((last % 60));
let res = '';
if(d>0){
res += `${d}`;
}
if(h !== '00'){
res += `${h}小时`;
}
if(m !== '00' || h !== '00'){
res += `${m}`;
}
res += `${s}`;
return res;
}
// 秒转时分秒
export function formatSecondsToHours(value) {
let result = parseInt(value)
let h = Math.floor(result / 3600);
let m = Math.floor((result / 60 % 60));
let s = Math.floor((result % 60));
let res = '';
if(h>0){
res += `${h}小时`;
}else{
if(m>0){
let mm=Math.floor(m /60);
res += `${mm}小时`;
}else{
res += '0小时';
}
}
return res;
}