角度2:12小时排序

时间:2018-05-24 05:08:43

标签: angular sorting

我正在尝试对12小时时间数组进行排序,

我使用此代码将24小时时间分类

 this.timings.Mon.sort( function(name1, name2) {
        if ( name1.open_time_time < name2.open_time_time ){
            return -1;
        }else if( name1.open_time_time > name2.open_time_time ){
            return 1;
        }else{
            return 0;
        }
 });

问题是我正在使用时刻将时间转换为HH:mm,并且该值已经排序(它也可以)。但是当它被转换时,它不能使用datepipe

显示为AM / PM
  this.timings.Mon = res.timings.filter(x => x.open_time && x.close_time && x.day === 'Mon').map(function (x) {

            return { id: x.id, open_time_time: moment(moment.utc(x.open_time).toDate()).format('HH:mm'), open_time: x.open_time, close_time_time: 
moment(moment.utc(x.close_time).toDate()).format('HH:mm'), close_time: x.close_time, working: x.working };
            });

示例时间列表是

4时45

09:00

15:00

21:00

21:30

(我按照24小时格式的排序顺序获取它)

有没有办法对12小时的时间进行分类,或以12小时的格式隐藏和显示分类的24小时日期?

1 个答案:

答案 0 :(得分:0)

这是接受Date的timeStamp的方法,它将以12 HR格式返回TIME

function TimeFormat (value) {
            if (!value) { return ''; }
            var hours = new Date(value).getHours();
            var minutes = new Date(value).getMinutes();
            var ampm = hours >= 12 ? 'PM' : 'AM';
            hours = hours % 12;
            hours = hours ? hours : 12; // the hour '0' should be '12'
            minutes = minutes < 10 ? '0' + minutes : minutes;
            var strTime = hours + ':' + minutes + ' ' + ampm;
            return strTime;
        }

这是working example