shy2850
2015-04-09 08:22:51
证
javascript日期时间操作
;(function (root, name ,factory) { if (typeof define === 'function' && define.amd) { define(name, factory); } else { root.DateUtil = factory(); } }(this,"dateUtil",function(require, exports, module) { //两位整数格式化,小于10高位补零 var fmt_num = function(n){ return n < 10 ? "0" + n : n; }; var _ = { reg : /([yMdhms\$]{1,2})/g, days :["星期日","星期一","星期二","星期三","星期四","星期五","星期六"], yy : function(d){return d.getFullYear()}, M : function(d){return 1+d.getMonth()}, MM : function(d){return fmt_num(1+d.getMonth())}, d : function(d){return d.getDate()}, dd : function(d){return fmt_num(d.getDate())}, h : function(d){return d.getHours()}, hh : function(d){return fmt_num(d.getHours())}, m : function(d){return d.getMinutes()}, mm : function(d){return fmt_num(d.getMinutes())}, s : function(d){return d.getSeconds()}, ss : function(d){return fmt_num(d.getSeconds())}, $ : function(d){return this.days[d.getDay()]}, $$ : function(d){return this.days[d.getDay()]} }; return { format : function(date,format,rule){ var m = {}; for(var k in _){ if( rule ){ m[k] = rule[k] || _[k] }else{ m[k] = _[k] } } return format.replace(_.reg,function(match,key){ return m[key](date); }); }, parse : function(str,format){ format = format || "yy/MM/dd hh:mm:ss"; //没有定义格式的话, 使用默认的格式 var map = {}, nums = str.match( /\d{1,4}/g ), fmts = format.match( _.reg ); for (var i = 0; i < fmts.length; i++) { map[ fmts[i] ] = nums[i]; }; //for循环完成格式和数据的对应关系。 //完成替换并且返回创建的Date结果。 return new Date( "yy/MM/dd hh:mm:ss".replace(_.reg,function(match,key){ return map[key] || 0; }) ); } } }));
TEST: test.js
var DU = require("./").DateUtil; var d = new Date(), str; console.log( 'Format: "yy年M月d日" \t-' + DU.format(d,"yy年M月d日") ); console.log( 'Format: "yy年MM月dd日 $" \t-' + DU.format(d,"yy年MM月dd日 $") ); console.log( 'Format: "yy年MM月dd日 $" \t-' + DU.format(d,"yy年MM月dd日 $",{days:"周日,周一,周二,周三,周四,周五,周六".split(",")}) ); console.log( 'Format: "yy年M月d日 h:m:s" \t-' + DU.format(d,"yy年M月d日 h:m:s") ); console.log( 'Format: "yy年MM月dd日 hh:mm:ss"\t-' + (str = DU.format(d,"yy年MM月dd日 hh:mm:ss") ) ); console.log( 'Parse: "' + str + '":\n\t' + DU.parse(str, "yy年MM月dd日 hh:mm:ss") );
AMD 引入支持 amd.html
require.config({ paths:{ "dateUtil": "index" } }); require(["dateUtil"],function(DateUtil){ var DU = DateUtil, pre = document.getElementById('pre'), time = document.getElementById('time'), log = function(s) { pre.innerHTML += s + "\n"; }; var d = new Date(), str; setInterval(function(){ time.innerHTML = DU.format(new Date,"yy年MM月dd日 hh:mm:ss $"); },300); log( 'Format: "yy年M月d日" \t-' + DU.format(d,"yy年M月d日") ); log( 'Format: "yy年MM月dd日 $" \t-' + DU.format(d,"yy年MM月dd日 $") ); log( 'Format: "yy年M月d日 h:m:s" \t-' + DU.format(d,"yy年M月d日 h:m:s") ); log( 'Format: "yy年MM月dd日 hh:mm:ss"\t-' + (str = DU.format(d,"yy年MM月dd日 hh:mm:ss") ) ); log( 'Parse: "' + str + '":\n\t' + DU.parse(str, "yy年MM月dd日 hh:mm:ss") ); });
猜你喜欢
请下载代码后再发表评论
文件名:date-util.rar,文件大小:2.541K
下载
- /
- /date-util
- /date-util/amd.html
- /date-util/index.js
- /date-util/simple.html
- /date-util/test.js
- /date-util


SamGray LV1
7月10日
JohnAlex LV9
5月20日
明月星 LV4
4月14日
511076827 LV24
4月11日
3188681084 LV1
3月24日
644800782 LV4
3月8日
lingtiejing LV14
2月25日
1798672867 LV21
1月22日
abcdljw LV16
2021年12月28日
比刘能还能 LV2
2021年12月5日