angularjs自定义日期过滤器,如:周日(前天 21:24)
2017-01-10 10:03
489 查看
今天给大家分享一个,我在项目中自定义的一个日期过滤器。具体过滤出来的效果可参看下图:
用法:
filter:
用法:
{{ time | timeFilter }}
filter:
App.filter('timeFilter', function() { // 日期格式化 //获取相对日期 function GetRelativeDate(timestampstr) { var timestamp = parseInt(timestampstr); timestamp = isNaN(timestamp) ? 0 : timestamp; var thenT = new Date(timestamp); thenT.setHours(0); thenT.setMinutes(0); thenT.setSeconds(0); var nowtime = new Date(); nowtime.setHours(0); nowtime.setMinutes(0); nowtime.setSeconds(0); var delt = Math.round((nowtime.getTime() - thenT.getTime()) / 1000 / 86400); var day_def = { '-3': '大后天', '-2': '后天', '-1': '明天', '0': '今天', '1': '昨天', '2': '前天', '3': '上前天' }[delt.toString()] || ((delt >= -30 && delt < 0) ? Math.abs(delt) + '天后' : (delt > 0 && delt <= 30) ? delt + '天前' : GetDateString(timestamp)); return day_def; } function GetDateString(timestampstr, split) { var timestamp = parseInt(timestampstr); timestamp = isNaN(timestamp) ? 0 : timestamp; var datetime = new Date(timestamp); var month = datetime.getMonth() + 1; var date = datetime.getDate(); if (split === undefined) split = '-'; return datetime.getFullYear() + split + (month > 9 ? month : "0" + month) + split + (date > 9 ? date : "0" + date); } return function(time) { var week = new Date(parseInt(time) * 1000).getDay(); var hours = new Date(parseInt(time) * 1000).getHours(); var minutes = new Date(parseInt(time) * 1000).getMinutes(); if(hours < 10 && minutes < 10) { var t = '0' + hours + ':0' + minutes; }else if(hours < 10 && minutes > 9) { var t = '0' + hours + ':' + minutes; }else if(hours > 9 && minutes < 10) { var t = hours + ':0' + minutes; }else { var t = hours + ':' + minutes; } switch(week) { case 1: return '周一('+GetRelativeDate(time * 1000)+' '+ t +')'; case 2: return '周二('+GetRelativeDate(time * 1000)+' '+ t +')'; case 3: return '周三('+GetRelativeDate(time * 1000)+' '+ t +')'; case 4: return '周四('+GetRelativeDate(time * 1000)+' '+ t +')'; case 5: return '周五('+GetRelativeDate(time * 1000)+' '+ t +')'; case 6: return '周六('+GetRelativeDate(time * 1000)+' '+ t +')'; case 0: return '周日('+GetRelativeDate(time * 1000)+' '+ t +')'; } }; });
相关文章推荐
- angularjs自定义过滤器
- AngularJS学习记录-过滤器(自定义过滤器)
- AngularJS 自定义过滤器
- 【AngularJS】—— 9 自定义过滤器
- vue2.0 自定义日期时间过滤器
- 详解AngularJS中自定义过滤器
- 使用AngularJS创建自定义的过滤器的方法
- AngularJS自定义过滤器用法经典实例总结
- angularjs自定义过滤器
- angularJs自定义过滤器实现手机号信息隐藏的方法
- angularJS自定义过滤器筛选列表/下拉框
- angularJs自定义过滤器
- angularjs 自定义过滤器filter和用法
- AngularJs自定义过滤器
- 【AngularJS】—— 9 自定义过滤器
- AngularJs自定义过滤器
- angularjs中的filter(过滤器)——格式化日期的date
- AngularJs学习笔记__6、自定义过滤器,阿拉伯数字金额转汉字金额
- AngularJs自定义过滤器
- angularJs-----$filter过滤器使用 自定义过滤器