Ionic2 将军用时间转换为离子2中的标准时间

Ionic2 将军用时间转换为离子2中的标准时间,ionic2,angular2-template,datetime-conversion,Ionic2,Angular2 Template,Datetime Conversion,这段JS是我在网上找到的一个日期格式化解决方案,它工作得很好,但是它不包括将时间从军事时间转换为标准时间的方法,也不包括PM/AM差异。我该怎么补充呢 HTML 日期:{{getFormattedDate(event.eventTime,'MM dd yyyy')} 时间:{{getFormattedDate(event.eventTime,'HH:MM')} .TS文件 getFormattedTime (time, format) { var t = new Da

这段JS是我在网上找到的一个日期格式化解决方案,它工作得很好,但是它不包括将时间从军事时间转换为标准时间的方法,也不包括PM/AM差异。我该怎么补充呢

HTML


日期:{{getFormattedDate(event.eventTime,'MM dd yyyy')}
时间:{{getFormattedDate(event.eventTime,'HH:MM')}
.TS文件

getFormattedTime (time, format) {
            var t = new Date(time);
            var tf = function (i) { return (i < 10 ? '0' : '') + i };
            return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
                switch (a) {
                    case 'yyyy':
                        return tf(t.getFullYear());

                    case 'MM':
                        return tf(t.getMonth() + 1);

                    case 'mm':
                        return tf(t.getMinutes());

                    case 'dd':
                        return tf(t.getDate());

                    case 'HH':
                        return tf(t.getHours());

                    case 'ss':
                        return tf(t.getSeconds());

                }
            })
        }// end of getFormattedTime 
getFormattedTime(时间,格式){
var t=新日期(时间);
var-tf=函数(i){return(i<10?'0':'')+i};
返回格式。替换(/yyyy | MM | dd | HH | MM | ss/g,函数(a){
开关(a){
案例“yyy”:
返回tf(t.getFullYear());
案例“MM”:
返回tf(t.getMonth()+1);
案例“mm”:
返回tf(t.getMinutes());
案例“dd”:
返回tf(t.getDate());
案例“HH”:
返回tf(t.getHours());
案例“ss”:
返回tf(t.getSeconds());
}
})
}//getFormattedTime结束

检查此回购协议,它将为您省去很多麻烦。只是给你一个介绍,当你想在js文件上处理日期时,momentjs是最好的js库,repo为你提供了这些和很多准备就绪的管道。你试过angular2日期管道吗?
getFormattedTime (time, format) {
            var t = new Date(time);
            var tf = function (i) { return (i < 10 ? '0' : '') + i };
            return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
                switch (a) {
                    case 'yyyy':
                        return tf(t.getFullYear());

                    case 'MM':
                        return tf(t.getMonth() + 1);

                    case 'mm':
                        return tf(t.getMinutes());

                    case 'dd':
                        return tf(t.getDate());

                    case 'HH':
                        return tf(t.getHours());

                    case 'ss':
                        return tf(t.getSeconds());

                }
            })
        }// end of getFormattedTime