export class StringAssist {
static NoHtml(html: string): string {
return html;
}
static PhoneToStr (e:string) {
return e.substring(0,3)+'****'+e.substring((e.length-2),(e.length))
}
// 数量过万处理
static NumToStr (sum:number) {
if(sum>=10000){
return (sum/10000).toFixed('2')+'w'
}else{
return sum
}
}
// 帖子距离现在多久
static DiffTimeTostring(dateTime: string): string {
let result = 0;
let time = Date.parse(dateTime);
let timestamp = Date.parse(new Date().toString());
if ((timestamp - time) / 1000 < 60) {
result = (timestamp - time) / 1000;
result = result < 0 ? 0 : result;
return result.toFixed(0) + '秒前';
} else if ((timestamp - time) / 1000 / 60 < 60) {
return ((timestamp - time) / 1000 / 60).toFixed(0) + '分钟前';
} else if ((timestamp - time) / 1000 / 60 / 60 < 24) {
return ((timestamp - time) / 1000 / 60 / 60).toFixed(0) + '小时前';
} else if ((timestamp - time) / 1000 / 60 / 60 / 24 < 31) {
return ((timestamp - time) / 1000 / 60 / 60 / 24).toFixed(0) + '天前';
} else {
return this.formatDate(time, 1);
}
}
private static formatDate(time:any,type:number):string
{
const date = new Date(time);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始,所以加1,并用0填充
const day = String(date.getDate()).padStart(2, '0'); // 用0填充
const hours = String(date.getHours()).padStart(2, '0'); // 用0填充
const minutes = String(date.getMinutes()).padStart(2, '0'); // 用0填充
const seconds = String(date.getSeconds()).padStart(2, '0'); // 用0填充
if(type==0)
{
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
else if(type==1)
{
return `${year}-${month}-${day} ${hours}:${minutes}`;
}else if(type==2)
{
return `${year}-${month}-${day}`;
}else if(type==3){
return `${month}-${day} ${hours}:${minutes}`;
}
else{
return `${hours}:${minutes}`;
}
}
// 聊天时间显示
static ChatTimeTostring(dateTime: string,upTime:string): string {
let time = Date.parse(dateTime);
let timestamp = Date.parse(upTime);
if (( time - timestamp) / 1000 / 60 < 10) {
return '0';
} else{
return this.formatDate(time, 3);
}
}
// 去除两侧的空格
static trim(str:string){
const reg = /^\s+|\s+$/g;
return str.replace(reg,'');
}
// 计算两个时分秒差值
static timesfm(dateTime: string,upTime:string){
let time = Date.parse('2000-01-01 '+dateTime);
let timestamp = Date.parse('2000-01-01 '+upTime);
return ((timestamp - time) / 1000 / 60).toFixed(0) + '分钟';
}
}