Angularjs中比较实用的DateFormat库
2024-09-07 14:33:03
angular.module('newApp')
.factory('dateUtil', function() {
var symbolMap = {
'MM': function(date) {
if (typeof(date) === 'string') {
var d = new Date(date);
return d.getMonth();
}
return date.getMonth() + 1;
},
'mm': function(date) {
if (typeof(date) === 'string') {
var d = new Date(date);
return d.getMinutes();
}
return date.getMinutes();
},
'YY': function(date) {
if (typeof(date) === 'string') {
var d = new Date(date);
return d.getFullYear();
}
return date.getFullYear();
},
'ss': function(date) {
if (typeof(date) === 'string') {
var d = new Date(date);
return d.getSeconds();
}
return date.getSeconds();
},
'hh': function(date) {
if (typeof(date) === 'string') {
var d = new Date(date);
return d.getHours();
}
return date.getHours();
},
'dd': function(date) {
if (typeof(date) === 'string') {
var d = new Date(date);
return d.getDate();
}
return date.getDate();
}
}; function _makeNchar(char, n) {
var str = [];
while (n--) {
str.push(char);
}
return str.join('');
} function alignNumber(num, len, char) {
num = num + '';
if (num.length > len) {
return num;
} else {
return _makeNchar(char, len - num.length) + num;
}
} function getRelativeDate(offset, date) {
var relativeDate = new Date(date),
dateValue = relativeDate.getDate() + offset;
relativeDate.setDate(dateValue);
return relativeDate;
}return {
format: function(date, fmtStr) {
if (fmtStr) {
return fmtStr.replace((/(MM|mm|YY|ss|hh|dd)/g), function(s) {
return alignNumber(symbolMap[s](date), 2, '0');
});
}
},
getRelativeDate: getRelativeDate
};
});
最新文章
- AMap公交线路查询
- CSS3——动画效果
- 【BZOJ-3293&;1465&;1045】分金币&;糖果传递×2 中位数 + 乱搞
- hdu 2052
- single page
- PHP 字符串替换 substr_replace 与 str_replace 函数
- codeblocks 使用指南z
- Linux入门基础 #10:命令行文本处理工具
- 经excel要将数据库(ORACLE)要插入数据
- 50行代码实现的高性能动画定时器 raf-interval
- CF每日一练 Codeforces Round #520 (Div. 2)
- url的参数解析成key-value
- 如何将他人的SOPC工程转换为自己可以使用的工程
- numpy元素级数组函数
- 实力封装:Unity打包AssetBundle(番外篇)
- 并发编程之 线程协作工具 LockSupport
- Android WebView漏洞(转)
- 《Java性能优化权威指南》
- 关于直播学习笔记-004-nginx-rtmp、srs、vlc、obs
- rertful规范