MCPcopy Create free account
hub / github.com/zxlie/FeHelper / formatDate

Function formatDate

apps/json-format/json-utils.js:358–390  ·  view source on GitHub ↗
(date, pattern)

Source from the content-addressed store, hash-verified

356
357// ─── 日期格式化(替代 Date.prototype.format 的纯函数版本)──
358export function formatDate(date, pattern) {
359 const pad = (src, len) => {
360 const neg = src < 0;
361 let s = String(Math.abs(src));
362 while (s.length < len) s = '0' + s;
363 return (neg ? '-' : '') + s;
364 };
365 if (typeof pattern !== 'string') return date.toString();
366
367 const y = date.getFullYear();
368 const M = date.getMonth() + 1;
369 const d = date.getDate();
370 const H = date.getHours();
371 const m = date.getMinutes();
372 const s = date.getSeconds();
373 const S = date.getMilliseconds();
374
375 return pattern
376 .replace(/yyyy/g, pad(y, 4))
377 .replace(/yy/g, pad(parseInt(y.toString().slice(2), 10), 2))
378 .replace(/MM/g, pad(M, 2))
379 .replace(/M/g, M)
380 .replace(/dd/g, pad(d, 2))
381 .replace(/d/g, d)
382 .replace(/HH/g, pad(H, 2))
383 .replace(/H/g, H)
384 .replace(/hh/g, pad(H % 12, 2))
385 .replace(/h/g, H % 12)
386 .replace(/mm/g, pad(m, 2))
387 .replace(/ss/g, pad(s, 2))
388 .replace(/SSS/g, pad(S, 3))
389 .replace(/S/g, S);
390}
391
392// ─── 字符串字节数(替代 String.prototype.getBytes 的纯函数版本)──
393export function getStringBytes(str) {

Callers 1

json-utils.test.jsFile · 0.90

Calls 1

padFunction · 0.70

Tested by

no test coverage detected