| 1 |
( function () { |
| 2 |
var FORMAT_MAP = { |
| 3 |
'long-date': { opts: { weekday:'long', year:'numeric', month:'long', day:'numeric' } }, |
| 4 |
'short-date': { opts: { year:'numeric', month:'short', day:'numeric' } }, |
| 5 |
'numeric-date': { opts: { year:'numeric', month:'2-digit', day:'2-digit' } }, |
| 6 |
'year-only': null, |
| 7 |
'time-12': { opts: { hour:'numeric', minute:'2-digit', hour12: true } }, |
| 8 |
'time-24': { opts: { hour:'2-digit', minute:'2-digit', hour12: false } }, |
| 9 |
'datetime': { opts: { year:'numeric', month:'short', day:'numeric', hour:'numeric', minute:'2-digit' } } |
| 10 |
}; |
| 11 |
|
| 12 |
function formatDate( format, tz ) { |
| 13 |
var now = new Date(); |
| 14 |
if ( format === 'year-only' ) return String( now.getFullYear() ); |
| 15 |
var entry = FORMAT_MAP[ format ]; |
| 16 |
if ( !entry ) return now.toLocaleDateString(); |
| 17 |
try { |
| 18 |
var opts = Object.assign( {}, entry.opts ); |
| 19 |
if ( tz && tz !== 'site' && tz !== 'utc' ) opts.timeZone = tz; |
| 20 |
if ( tz === 'utc' ) opts.timeZone = 'UTC'; |
| 21 |
return now.toLocaleString( navigator.language || 'en', opts ); |
| 22 |
} catch (e) { |
| 23 |
return now.toLocaleString(); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
document.querySelectorAll( '.bkdd-wrap' ).forEach( function ( wrap ) { |
| 28 |
var format = wrap.dataset.format || 'long-date'; |
| 29 |
var tz = wrap.dataset.tz || 'site'; |
| 30 |
var live = wrap.dataset.live === '1'; |
| 31 |
var dateEl = wrap.querySelector( '.bkdd-date' ); |
| 32 |
if ( !dateEl ) return; |
| 33 |
|
| 34 |
function update() { |
| 35 |
dateEl.textContent = formatDate( format, tz ); |
| 36 |
} |
| 37 |
update(); |
| 38 |
|
| 39 |
if ( live ) { |
| 40 |
setInterval( update, 1000 ); |
| 41 |
} |
| 42 |
} ); |
| 43 |
} )(); |
| 44 |
|