<body>
<div?id="date"></div>
</body>
</html>
<script>
????function?dateFormat(date,format)?{
????????var?o?=?{
????????????"M+"?:?date.getMonth()+1,??????????????????//month
????????????"d+"?:?date.getDate(),?????????????????????//day
????????????"h+"?:?date.getHours(),????????????????????//hour
????????????"m+"?:?date.getMinutes(),??????????????????//minute
????????????"s+"?:?date.getSeconds(),??????????????????//second
????????????"q+"?:?Math.floor((date.getMonth()+3)/3),??//quarter
????????????"S"?:?date.getMilliseconds()???????????????//millisecond
????????}
????????if(/(y+)/.test(format))?format=format.replace(RegExp.$1,
????????????(date.getFullYear()+"").substr(4-?RegExp.$1.length));
????????for(var?k?in?o)if(new?RegExp("("+?k?+")").test(format))
????????????format?=?format.replace(RegExp.$1,
????????????????RegExp.$1.length==1??o[k]?:
????????????????????("00"+?o[k]).substr((""+?o[k]).length));
????????return?format;
????}
????//
????function?Time()?{
????????this.produceDate?=?'';
????????Object.defineProperty(this,?"produceDate",?{
????????????get:?function?()?{
????????????????return?dateFormat(produceDate,'yyyy-MM-dd');
????????????},
????????????set:?function?(value)?{
????????????????produceDate?=?value;
????????????}
????????});
????}
????/*獲取元素*/
????var?date?=?document.getElementById('date')
????//?實例化
????var?today?=?new?Time();
????today.produceDate=new?Date();
????date.innerHTML=today.produceDate;
</script>里面的方法我都明白,但是下面這一塊的思路不是很清晰?這一段的if語句和for循環(huán)都沒有加花括弧,不知道加和不加到底有什么區(qū)別,這樣寫的意義在哪里?望大神解答一下,非常感謝!if(/(y+)/.test(format)) format=format.replace(RegExp.$1,(date.getFullYear()+"").substr(4- RegExp.$1.length));for(var k in o)if(new RegExp("("+ k +")").test(format))format = format.replace(RegExp.$1,RegExp.$1.length==1? o[k] :("00"+ o[k]).substr((""+ o[k]).length));return format;
一段有關于用正則匹配日期的代碼?
kumayato
2017-03-30 15:23:05