You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.3 KiB
36 lines
1.3 KiB
export function isValidIPv4(ip: string) {
|
|
const ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
|
|
return ipv4Regex.test(ip);
|
|
}
|
|
export function formatRemainTime(seconds: number) {
|
|
const min = Math.floor(seconds / 60).toFixed();
|
|
const sec = (seconds % 60).toFixed();
|
|
return min.padStart(2, "0") + ":" + sec.padStart(2, "0");
|
|
}
|
|
|
|
export function timestampToDate(timestamp:number, format = 'YYYY-MM-DD HH:mm:ss') {
|
|
// 创建 Date 对象
|
|
const date = new Date(timestamp);
|
|
|
|
// 如果时间戳无效,返回错误信息
|
|
if (isNaN(date.getTime())) {
|
|
return '时间戳无效';
|
|
}
|
|
|
|
// 提取日期和时间部分
|
|
const year = String(date.getFullYear())
|
|
const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始,需要加 1
|
|
const day = String(date.getDate()).padStart(2, '0');
|
|
const hours = String(date.getHours()).padStart(2, '0');
|
|
const minutes = String(date.getMinutes()).padStart(2, '0');
|
|
const seconds = String(date.getSeconds()).padStart(2, '0');
|
|
|
|
// 根据传入的格式返回日期字符串
|
|
return format
|
|
.replace('YYYY', year)
|
|
.replace('MM', month)
|
|
.replace('DD', day)
|
|
.replace('HH', hours)
|
|
.replace('mm', minutes)
|
|
.replace('ss', seconds);
|
|
}
|