angularjs自定义日期过滤器,如:周日(前天 21:24)

2017-01-12 19:08:37来源:作者:人点击

今天给大家分享一个,我在项目中自定义的一个日期过滤器。具体过滤出来的效果可参看下图:



用法:

{{ 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 +')';} };});

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台