求助一段代码,看不懂

wudichaoren2011 2015-09-08 12:15:03


// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}调用: var time1 = new Date().Format("yyyy-MM-dd");var time2 = new Date().Format("yyyy-MM-dd HH:mm:ss");



求助


"M+": this.getMonth() + 1, //月份



M+这里是什么意思?是变量还是字符,为什么这么用?


/(y+)/.test(fmt)



/(y+)/为什么加/ / ,这个的作用是什么?
...全文
244 11 打赏 收藏 举报
写回复
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
吴钦飞 2015-09-13
  • 打赏
  • 举报
回复

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>

<body>
    <script>
    // 对Date的扩展,将 Date 转化为指定格式的String
    // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, 
    // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
    // 例子: 
    // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
    // (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
    Date.prototype.Format = function(fmt) { //author: meizz 
        // "M+"、"d+"、"s+" 都是要被转换化为正则对象的
        // 对象{name:value,...} 的 名值对(name:value) 中的 name 的命名规则:
        //    1. 标识符(跟变量的命名规则一致);
        //    2. 任意字符串(如"","@#"),例, ({"@#":1})["@#"]
        var o = {
            "M+": this.getMonth() + 1, //月份 
            "d+": this.getDate(), //日 
            "h+": this.getHours(), //小时 
            "m+": this.getMinutes(), //分 
            "s+": this.getSeconds(), //秒 
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
            "S": this.getMilliseconds() //毫秒 
        };

        // fmt = "yyyy-MM-dd"
        // 现在时间:2015-09-13 11:29:36

        if ( /(y+)/.test( fmt ) ) { // 测试 匹配字符串fmt("yyyy-MM-dd")是否包含“y”
          // RegExp.$1 : 代表“yyyy”,将被替换为“2015”
          // 如果fmt的年份是2位则取后两位,如 09/13/15(月月日日年年)
          var year = ( this.getFullYear() + "" ).substr( 4 - RegExp.$1.length ); // year = "2015"
          fmt = fmt.replace( RegExp.$1 , year ); // fmt = "2015-MM-dd"
        };
        for ( var k in o ) {
          // 创建正则对象的两种方式:
          //  1,var regex = /[1-9]{1,7}/; 
          //  2. var regex = new RegExp( "[1-9]{1,7}" );
          if ( new RegExp( "(" + k + ")") .test( fmt ) ) { 
            var temp = ( RegExp.$1.length == 1 ) ? // 是否检测到 毫秒的部分("S")
                       ( o[ k ] ) 
                       : 
                       ( ( "00" + o[k] ).substr( ( "" + o[k] ).length ) ); // 补零
            fmt = fmt.replace( RegExp.$1, temp )
          };
        }
        return fmt;
    }
    // 调用:
    var time1 = new Date().Format("yyyy-MM-dd");
    var time2 = new Date().Format("yyyy-MM-dd hh:mm:ss");
    console.info( time1, time2 ); //=> 2015-09-13 2015-09-13 11:29:36
    </script>
</body>

</html>
lingyp 2015-09-13
  • 打赏
  • 举报
回复
"M+": this.getMonth() + 1, //月份 是键值对,键名"M+"
wudichaoren2011 2015-09-13
  • 打赏
  • 举报
回复
/(y+)/ 代表什么,正则里面没看到
wudichaoren2011 2015-09-13
  • 打赏
  • 举报
回复


new RegExp("(" + k + ")")
这里为什么不直接用new RegExp( k ) 实在看不懂
-妖孽 2015-09-09
  • 打赏
  • 举报
回复
注释不是都已经写好了么
天际的海浪 2015-09-09
  • 打赏
  • 举报
回复
引用 5 楼 wudichaoren2011 的回复:
[quote=引用 2 楼 jiangbai333 的回复:] 1:双引号内是字符串,然而这个串只有一个字符,M 月份 的简写。 2:正则表达式定界符,你别问我为啥这么用,这是就这么规定的,就好问为什么人有两条腿一样,就必须这么用
为什么是M+而不是M


<script language="javascript" type="text/javascript">

Date.prototype.format = function(format)
{
	
	var o = { 
	"M+" : this.getMonth()+1, //month 
	"d+" : this.getDate(), //day 
	"h+" : this.getHours(), //hour 
	"m+" : this.getMinutes(), //minute 
	"s+" : this.getSeconds(), //second 
	"q+" : Math.floor((this.getMonth()+3)/3), //quarter 
	"S" : this.getMilliseconds() //millisecond 
	}
	
	for(var k in o)
	{ 
	format =format+o[k]
	}
}


alert(new Date().format("yyyy年MM月dd日")); 
</script>

为什么这么写不对?[/quote] "M+"这个字符串最终会用于创建正则表达式,在正则表达式中 + 号匹配其前面的字符一次或多次。
wudichaoren2011 2015-09-08
  • 打赏
  • 举报
回复
引用 2 楼 jiangbai333 的回复:
1:双引号内是字符串,然而这个串只有一个字符,M 月份 的简写。 2:正则表达式定界符,你别问我为啥这么用,这是就这么规定的,就好问为什么人有两条腿一样,就必须这么用
为什么是M+而不是M


<script language="javascript" type="text/javascript">

Date.prototype.format = function(format)
{
	
	var o = { 
	"M+" : this.getMonth()+1, //month 
	"d+" : this.getDate(), //day 
	"h+" : this.getHours(), //hour 
	"m+" : this.getMinutes(), //minute 
	"s+" : this.getSeconds(), //second 
	"q+" : Math.floor((this.getMonth()+3)/3), //quarter 
	"S" : this.getMilliseconds() //millisecond 
	}
	
	for(var k in o)
	{ 
	format =format+o[k]
	}
}


alert(new Date().format("yyyy年MM月dd日")); 
</script>

为什么这么写不对?
li_huan_88 2015-09-08
  • 打赏
  • 举报
回复
"M+": this.getMonth() + 1, //月份 是键值对,键名"M+"
jiangbai333 2015-09-08
  • 打赏
  • 举报
回复
1:双引号内是字符串,然而这个串只有一个字符,M 月份 的简写。 2:正则表达式定界符,你别问我为啥这么用,这是就这么规定的,就好问为什么人有两条腿一样,就必须这么用
hch126163 2015-09-08
  • 打赏
  • 举报
回复
js 正则表达式
天际的海浪 2015-09-08
  • 打赏
  • 举报
回复
这都是“正则表达式” http://deerchao.net/tutorials/regex/regex.htm
发帖
JavaScript

8.7w+

社区成员

Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
帖子事件
创建了帖子
2015-09-08 12:15
社区公告
暂无公告