浅谈如何实现easyui的datebox格式化


看了网上有很多的解决方法,我也写一个比较简单方法。实现easyui的datebox格式化。效果如下,用“++”隔开,看你喜欢用什么都可以。

 

1、html

<span class="span1">证件有效期至:</span> 
<span><input id="passvali" name="hotel.passvali"></span> 

2、js

/* 证件有效期至 */
 $('#passvali').datebox({
 formatter: function(date){ 
 var years=date.getFullYear();//获取年
 var months=date.getMonth()+1;//获取日
 var dates=date.getDate();//获取月
 
 if(months<10){//当月份不满10的时候前面补0,例如09
 months='0'+months;
 }
 
 if(dates<10){//当日期不满10的时候前面补0,例如09
 dates='0'+dates;
 }
 return years+"++"+months+"++"+dates;//根据自己需求进行改动
 }
 });

补充:

DateBox 日期显示默认的格式为“dd/mm/yyyy”,如果想自定义成我们的格式需要实现两个函数,formatter和parser。
formatter函数使得选择日期后将其格式化为我们需要的格式,parser函数在选择好日期后告诉控件如何去解析我们自定义的格式。
定义如下:
formatter:A function to format the date, the function take a 'date' parameter and return a string value.
parser:A function to parse a date string, the function take a 'date' string and return a date value.
如将日期格式化为yyyy-mm-dd的格式:

$('#dd1').datebox({
  formatter: function(date){ return date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate();},
  parser: function(date){ return new Date(Date.parse(date.replace(/-/g,"/")));}
});

以上就是为大家分享的easyui的datebox格式化最简单方法,希望对大家的学习有所帮助。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3