基质喷涂
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.

35 lines
1.3 KiB

6 months ago
  1. export function isValidIPv4(ip: string) {
  2. 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]?)$/;
  3. return ipv4Regex.test(ip);
  4. }
  5. export function formatRemainTime(seconds: number) {
  6. const min = Math.floor(seconds / 60).toFixed();
  7. const sec = (seconds % 60).toFixed();
  8. return min.padStart(2, "0") + ":" + sec.padStart(2, "0");
  9. }
  10. export function timestampToDate(timestamp:number, format = 'YYYY-MM-DD HH:mm:ss') {
  11. // 创建 Date 对象
  12. const date = new Date(timestamp);
  13. // 如果时间戳无效,返回错误信息
  14. if (isNaN(date.getTime())) {
  15. return '时间戳无效';
  16. }
  17. // 提取日期和时间部分
  18. const year = String(date.getFullYear())
  19. const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始,需要加 1
  20. const day = String(date.getDate()).padStart(2, '0');
  21. const hours = String(date.getHours()).padStart(2, '0');
  22. const minutes = String(date.getMinutes()).padStart(2, '0');
  23. const seconds = String(date.getSeconds()).padStart(2, '0');
  24. // 根据传入的格式返回日期字符串
  25. return format
  26. .replace('YYYY', year)
  27. .replace('MM', month)
  28. .replace('DD', day)
  29. .replace('HH', hours)
  30. .replace('mm', minutes)
  31. .replace('ss', seconds);
  32. }