Spaces:
Sleeping
Sleeping
File size: 2,076 Bytes
4bea261 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | /**
* TXA Format Utility
* Version: 1.0 (Web Optimized)
*/
export const txaformat = {
twoDigits: (n) => (n < 10 ? '0' : '') + n,
shortNumber: (n) => {
if (n >= 1000000) return (n / 1000000).toFixed(1) + 'M';
if (n >= 1000) return (n / 1000).toFixed(1) + 'K';
return n;
},
number: (n) => new Intl.NumberFormat('vi-VN').format(n),
duration: function (s) {
const h = Math.floor(s / 3600);
const m = Math.floor((s % 3600) / 60);
const sec = Math.floor(s % 60);
let str = '';
if (h > 0) str += this.twoDigits(h) + ':';
return str + this.twoDigits(m) + ':' + this.twoDigits(sec);
},
fileSize: (bytes) => {
if (bytes === 0) return '0 B';
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(1024));
return parseFloat((bytes / Math.pow(1024, i)).toFixed(2)) + ' ' + units[i];
},
dateTime: (val) => {
if (!val) return '';
const d = new Date(val);
if (isNaN(d.getTime())) return val;
return d.toLocaleString('vi-VN');
},
relativeTime: (val) => {
if (!val) return '';
const date = new Date(val);
const now = new Date();
const diffMs = now.getTime() - date.getTime();
if (isNaN(date.getTime()) || diffMs < 0) return 'Vừa xong';
const diffSec = Math.floor(diffMs / 1000);
if (diffSec < 60) return 'Vừa xong';
const diffMin = Math.floor(diffSec / 60);
if (diffMin < 60) return `${diffMin} phút trước`;
const diffHour = Math.floor(diffMin / 60);
if (diffHour < 24) return `${diffHour} giờ trước`;
// Quá 1 ngày (24 giờ) sẽ hiện là ngày/tháng/năm
const day = date.getDate();
const month = date.getMonth() + 1;
const year = date.getFullYear();
return `${day}/${month}/${year}`;
}
};
if (typeof window !== 'undefined') {
window.txaformat = txaformat;
}
|