一.date过滤器的功能是基于要求的格式格式化一个日期成为一个字符串。
格式化字符串的基本参数:本文主要和大家分享angularjs时间格式化过滤器详解,希望能帮助到大家。
‘yyyy’: 用4位数字表示年(例如:ad 1 => 0001, ad 2010 => 2010)
‘yy’: 用两位数字表示年(00-99)(例如:ad 2001 => 01, ad 2010 => 10)
‘y’: 用一位数字代表年(例如:ad 1 => 1, ad 199 => 199)
‘mmmm’: 英文全称表示月(january-december)
‘mmm’: 英文缩写表示月(jan-dec)
‘mm’: 两位数字表示月(01-12)
‘m’: 月(1-12)
‘dd’: 两位数字表示日(01-31)
‘d’: 日(1-31)
‘eeee’: 英文全称的一周中的天(sunday-saturday)
‘eee’: 英文缩写的一周中的天(sun-sat)
‘hh’: 两位数表示24小时制的时(00-23)
‘h’: 24小时制的时(0-23)
‘hh’: 两位数字表示上午或下午的时(01-12)
‘h’: 上午或下午的时(1-12)
‘mm’: 两位数字表示分(00-59)
‘m’: 分(0-59)
‘ss’: 两位数字表示秒(00-59)
‘s’: 秒(0-59)
‘sss’: 毫秒(000-999)
‘a’: am/pm
‘z’: 4位数字(+符号)代表时区偏移量(-1200——+1200)
‘ww’: 用两位数字表示一年的周数(00-53),第一周(01)是一年中的第一个星期四
‘w’: 一年的周数(0-53),第一周(1)是一年中的第一个星期四
‘g’,’gg’,‘ggg’: 年代字符串的缩写形式,例如‘ad’(公元)
‘gggg’: 年代字符串的全称,例如‘anno domini’(公元)
上面这些参数我们可以根据自己的意愿自由组合得到自己想要的格式,例如'yyyy-mm-dd'等。
格式化字符串也提供了一些预定义的本地化格式,可以方便我们使用:
medium:‘mmm d,y h:mm:ss a’ 例如:sep 3, 2010 12:05:08 pm
short:‘m/d/yy h:mm a’ 例如: 9/3/10 12:05 pm
fulldate: ’eeee,mmmm d,y’ 例如:friday, september 3, 2010
longdate: ‘mmmm d,y’ 例如:september 3, 2010
mediumdate: ’mmm d,y’ 例如: sep 3, 2010
shortdate: ’m/d/y’ 例如: 9/3/10
mediumtime: ’h:mm:ss a’ 例如:12:05:08 pm
shorttime: ’h:mm a’ 例如:12:05 pm
格式化字符串可以包含文本值。这些需要被单引号包围(例如 “h ‘in the morning’”),如果想输出一对单引号,就在一个序列中用两个双引号(例如:“h ‘o’’clock’”)
date过滤器的用法:
1.在html中用法:{{ date_expression | date : format : timezone}}
实例:
<span>{{1288323623006 | date:'medium'}}</span><br>
<span>{{1288323623006 | date:'yyyy-mm-dd hh:mm:ss z'}}</span><br>
<span>{{'1288323623006' | date:'mm/dd/yyyy @ h:mma'}}</span><br>
<span>{{'1288323623006' | date:"mm/dd/yyyy 'at' h:mma"}}</span><br>
输出结果为:
oct 29, 2010 11:40:23 am
2010-10-29 11:40:23 +0800
10/29/2010 @ 11:40am
10/29/2010 at 11:40am
2.在javascript中的用法:$filter('date')(date, format, timezone)
实例:
var today = new date();
$scope.formatdate = $filter('date')(today, 'yyyy-mm-dd');
输出结果为:
2015-01-28
二、
angularjs的date过滤器可以将日期格式化成需要的格式。常用方式: <!-- 2016-12-03 10:43:51 -->
{{now | date : 'yyyy-mm-dd hh:mm:ss'}}如果没有指定格式,angularjs会采用默认格式mediumdate
{{data | date}}<!-- dec 3, 2016 -->
{{data | date : mediumdate}}<!-- dec 3, 2016 -->
下面是angularjs内置的日期格式化
{{ now | date:'medium' }}<!-- dec 3, 2016 10:43:51 am -->
{{ now | date:'short' }}<!-- 12/3/16 10:43 am -->
{{ now | date:'fulldate' }}<!-- saturday, december 3, 2016 -->
{{ now | date:'longdate' }}<!-- december 3, 2016 -->
{{ now | date:'mediumdate' }}<!-- dec 3, 2016 -->
{{ now | date:'shortdate' }}<!-- 12/3/16 -->
{{ now | date:'mediumtime' }}<!-- 10:43:51 am -->
{{ now | date:'shorttime' }}<!-- 10:43 am -->
年份格式化
四位年份: {{ now | date:'yyyy' }} 21f8b90508180e8fb6c080fc31eb23a9
两位年份: {{ now | date:'yy' }} 301ab69c48343e0c405282031c1ea155
一位年份: {{ now | date:'y' }} 21f8b90508180e8fb6c080fc31eb23a9
月份格式化
英文月份: {{ now | date:'mmmm' }} 8d5ab341928958b6fad0d6bbc40b4ff6
英文月份简写: {{ now | date:'mmm' }} effa84c4dbed9a1587593a4dc2ac5781
数字月份: {{ now |date:'mm' }} 2485a18c88644944777fda07f87a161f
一年中的第几个月份: {{ now |date:'m' }} 2485a18c88644944777fda07f87a161f
日期格式化
数字日期: {{ now | date:'dd' }} 9801ade335e8996c05c79bdcdb82ed99
一个月中的第几天: {{ now | date:'d' }} e7e62d5f97460753606b5fde4974d6a0
英文星期: {{ now | date:'eeee' }} 7736582078d4b632358284ad60e5af10
英文星期简写: {{ now | date:'eee' }} 9f2647462a40c5863361fde87163e007
小时格式化
24小时制数字小时: {{now | date:'hh'}} 292f637d1ac700e7fecb86f3cb405aa8
一天中的第几个小时: {{now | date:'h'}} 292f637d1ac700e7fecb86f3cb405aa8
12小时制数字小时: {{now | date:'hh'}} 9cb30901740ebee1bfd96717de66ab5f
上午或下午的第几个小时: {{now | date:'h'}} 9cb30901740ebee1bfd96717de66ab5f
分钟格式化
数字分钟数: {{ now | date:'mm' }} 7a8efe97822350462eeffeef20fe0281
一个小时中的第几分钟: {{ now | date:'m' }} 7a8efe97822350462eeffeef20fe0281
秒数格式化
数字秒数: {{ now | date:'ss' }} 278220e7656d1bc229bf0cdc1e0b18e9
一分钟内的第几秒: {{ now | date:'s' }} 278220e7656d1bc229bf0cdc1e0b18e9
毫秒数: {{ now | date:'.sss' }} af28386278419624fd78e3b58bed0fe5
字符格式化
上下午标识: {{ now | date:'a' }} 72d59f38bdb060b8d3546a2cc6772cd3
四位时区标识: {{ now | date:'z' }} aea2e2bc758129ccc68943f6f77c8a2c
自定义格式
{{ now | date:'mmmd, y' }} 052921f587e50bb63876abe312662bb8
{{ now | date:'eeee, d, m' }} 50f42f01209326b736a34e0ba8905567
{{ now | date:'hh:mm:ss.sss' }} cd9f541f15df5d0ef8d0e67a6922b774
相关推荐:
angularjs过滤器完成排序功能实例详解
angular js过滤器的详细介绍
详解angularjs过滤器实现动态搜索与排序功能
以上就是angularjs时间格式化过滤器详解的详细内容。
