Skip to content

Commit cbaf2b1

Browse files
xofeFlole998
authored andcommitted
webui: Fix year being replaced incorrectly when using custom date format
fixes regression in 2ca8a19
1 parent c631154 commit cbaf2b1

1 file changed

Lines changed: 19 additions & 21 deletions

File tree

src/webui/static/app/tvheadend.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,10 +1390,10 @@ tvheadend.toLocaleFormat = function()
13901390
tvheadend.toCustomDate = function(date, format) {
13911391
if (/(%[MmsSyYdhHIpPq]+)/.test(format)) {
13921392
const o = {
1393+
"%[yY]+": date.getFullYear(),
13931394
"%M+": date.getMonth() + 1,
13941395
"%d+": date.getDate(),
1395-
"%h+": date.getHours(),
1396-
"%H+": date.getHours(),
1396+
"%[hH]+": date.getHours(),
13971397
"%I+": date.getHours() % 12 || 12,
13981398
"%p": date.getHours() >= 12 ? "PM" : "AM",
13991399
"%P": date.getHours() >= 12 ? "pm" : "am",
@@ -1403,31 +1403,29 @@ tvheadend.toCustomDate = function(date, format) {
14031403
"%S": date.getMilliseconds()
14041404
};
14051405

1406-
format = format.replace(/(%[yY]+)/, (date.getFullYear() + "").substr(5 - RegExp.$1.length))
1407-
.replace(/(%MMMM)/, date.toLocaleDateString(tvheadend.toLocaleFormat(), { month: 'long' }))
1408-
.replace(/(%MMM)/, date.toLocaleDateString(tvheadend.toLocaleFormat(), { month: 'short' }))
1409-
.replace(/(%dddd)/, date.toLocaleDateString(tvheadend.toLocaleFormat(), { weekday: 'long' }))
1410-
.replace(/(%ddd)/, date.toLocaleDateString(tvheadend.toLocaleFormat(), { weekday: 'short' }));
1406+
format = format.replace(/%MMMM/, date.toLocaleDateString(tvheadend.toLocaleFormat(), { month: 'long' }))
1407+
.replace(/%MMM/, date.toLocaleDateString(tvheadend.toLocaleFormat(), { month: 'short' }))
1408+
.replace(/%dddd/, date.toLocaleDateString(tvheadend.toLocaleFormat(), { weekday: 'long' }))
1409+
.replace(/%ddd/, date.toLocaleDateString(tvheadend.toLocaleFormat(), { weekday: 'short' }));
14111410

14121411
for (const k in o) {
1413-
if (new RegExp("(" + k + ")").test(format)) {
1414-
format = format.replace(RegExp.$1, RegExp.$1.length == 2 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
1415-
}
1412+
// pad to 4 places with zero, then slice from the end 1 less than match length (to trim % char)
1413+
format = format.replace(new RegExp(k), (match) => match.length === 2 ? o[k] : String(o[k]).padStart(4, 0).slice(1 - match.length));
14161414
}
14171415

14181416
return format;
1419-
} else {
1420-
const options = {
1421-
month: '2-digit',
1422-
day: '2-digit',
1423-
year: 'numeric',
1424-
hour: '2-digit',
1425-
minute: '2-digit',
1426-
second: '2-digit',
1427-
hour12: false
1428-
};
1429-
return date.toLocaleString(tvheadend.toLocaleFormat(), options);
14301417
}
1418+
1419+
const options = {
1420+
month: '2-digit',
1421+
day: '2-digit',
1422+
year: 'numeric',
1423+
hour: '2-digit',
1424+
minute: '2-digit',
1425+
second: '2-digit',
1426+
hour12: false
1427+
};
1428+
return date.toLocaleString(tvheadend.toLocaleFormat(), options);
14311429
};
14321430

14331431
/**

0 commit comments

Comments
 (0)