几个有用的javascript(日期比较,数字验证,数字和汉字长度计算)
1:日期大Js代码
- //人员失效职位日期是否小于组织失效日期
- function perDateInvalidate(){
- var flag = true;
- //组织失效日期
- var orgDate = stringToTime(document.getElementById("orgDate").value);
- //人员失效日期
- var personDate = stringToTime(document.getElementById("personDate").value);
- var acount = caculateOverTime(personDate,orgDate);
- if(acount<1){
- flag = false;
- }
- return flag;
- }
- function stringToTime(dateStr){
- var str1 = dateStr.split(' ');
- var day = str1[0].split('-');
- //var time = str1[1].split(':');
- //var date = new Date(Number(day[0]),Number(day[1])-1,Number(day[2]),Number(time[0]),Number(time[1]));
- var date = new Date(Number(day[0]),Number(day[1])-1,Number(day[2]));
- return date;
- }
- //计算加班时间,参数为date类型
- function caculateOverTime(begin,end){
- // alert(begin+":"+end);
- var days;
- if(begin != null && end != null){
- days =(end-begin)/1000/3600/24;
- //少算一天
- //days = days + 1;
- return days;
- }else{
- return 0;
- }
- }
2:日期比较
orgDateTo:'2009-9-9',orgDateFrom:'2009-9-2'.
- var dateFormValue = new Date(Date.parse(orgDateFrom.replace(/-/g,"/")));
- var dateToValue = new Date(Date.parse(orgDateTo.replace(/-/g,"/")));
- if((dateToValue<dateFormValue)){
- Ext.Msg.alert('提示','"截止日期"不能早于"起始日期"!');
- return;
- }
3:数字验证:
- //数字验证
- function f_check_number(obj)
- {
- if (/^\d+$/.test(obj))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
4:得到字符串长度包括英文和汉字
- function getValueLen(nameValue){
- var nameStr = nameValue;
- var len = 0;
- for(var i=0; i<nameStr.length; i++){
- //str = markerStr.charAt(i);
- if(nameStr.charCodeAt(i)>255 || nameStr.charCodeAt(i)<0){
- len +=2;
- }else{
- len++;
- }
- }
- return len;
- }