Skip to content

Commit 2ca8a19

Browse files
pmatisFlole998
authored andcommitted
Add support for 12-hour custom date formats
1 parent 4430ee7 commit 2ca8a19

1 file changed

Lines changed: 37 additions & 27 deletions

File tree

‎src/webui/static/app/tvheadend.js‎

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,38 +1387,48 @@ tvheadend.toLocaleFormat = function()
13871387
return tvh_locale_lang.replace('_','-');
13881388
};
13891389

1390-
tvheadend.toCustomDate = function(date, format) //author: meizz, improvements by pablozg
1391-
{
1392-
if(/([%][MmsSyYdhq]+)/.test(format)){
1393-
var o = {
1394-
"\%M+" : date.getMonth()+1, //month
1395-
"\%d+" : date.getDate(), //day
1396-
"\%h+" : date.getHours(), //hour
1397-
"\%m+" : date.getMinutes(), //minute
1398-
"\%s+" : date.getSeconds(), //second
1399-
"\%q+" : Math.floor((date.getMonth()+3)/3), //quarter
1400-
"\%S" : date.getMilliseconds() //millisecond
1401-
}
1402-
1403-
if(/(\%[yY]+)/.test(format)) format=format.replace(RegExp.$1, (date.getFullYear()+"").substr(5 - RegExp.$1.length));
1404-
1405-
if(/(\%MMMM)/.test(format)) format=format.replace(RegExp.$1, (date.toLocaleDateString(tvheadend.toLocaleFormat(), {month: 'long'})));
1406-
1407-
if(/(\%MMM)/.test(format)) format=format.replace(RegExp.$1, (date.toLocaleDateString(tvheadend.toLocaleFormat(), {month: 'short'})));
1390+
tvheadend.toCustomDate = function(date, format) {
1391+
if (/(%[MmsSyYdhHIpPq]+)/.test(format)) {
1392+
const o = {
1393+
"%M+": date.getMonth() + 1,
1394+
"%d+": date.getDate(),
1395+
"%h+": date.getHours(),
1396+
"%H+": date.getHours(),
1397+
"%I+": date.getHours() % 12 || 12,
1398+
"%p": date.getHours() >= 12 ? "PM" : "AM",
1399+
"%P": date.getHours() >= 12 ? "pm" : "am",
1400+
"%m+": date.getMinutes(),
1401+
"%s+": date.getSeconds(),
1402+
"%q+": Math.floor((date.getMonth() + 3) / 3),
1403+
"%S": date.getMilliseconds()
1404+
};
14081405

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

1411-
if(/(\%ddd)/.test(format)) format=format.replace(RegExp.$1, (date.toLocaleDateString(tvheadend.toLocaleFormat(), {weekday: 'short'})));
1412+
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+
}
1416+
}
14121417

1413-
for(var k in o)
1414-
if(new RegExp("("+ k +")").test(format))
1415-
format = format.replace(RegExp.$1, RegExp.$1.length==2 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
14161418
return format;
1417-
}else{
1418-
var options = {weekday: 'short', day: '2-digit', month: '2-digit', year: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false};
1419-
return date.toLocaleString(tvheadend.toLocaleFormat(), options);
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(toLocaleFormat(), options);
14201430
}
1421-
}
1431+
};
14221432

14231433
/**
14241434
*

0 commit comments

Comments
 (0)