`
kokorodo
  • 浏览: 37626 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
日期处理工具类
/**
 * 工具类
 * 该类中如果方法名称带前缀“_”的(如_checkType)
 * 都是私有方法,一般不建议外部调用。
 */
var xh = {

	DateUtils : {
		
		/**
		 * 检查对象的类型,目前要求只能传入string或者Date,
		 * 检查完成后返回Date类型 
		 * @param {} args
		 * @return {}
		 */
		_checkType : function(args){
			/* 

			 */
			
			if (typeof args === 'string'){
				if (this.validate(args)){
					return this.convert(args);
				} else {
					alert('日期时间格式错误!\n[' + args + ']');
					throw new Error('日期时间格式错误!\n[' + args + ']');
				}
			} else if (typeof args === 'object'){
				if (args instanceof Date){
					return args;	
				} else {
					alert('日期时间类型错误!');
					throw new Error('日期时间类型错误!');
				}
					
			} else {
				alert('日期时间类型错误!');
				throw new Error('日期时间类型错误!');
			}
		},
		
		
		/**
		 * 检查日期格式
		 * yyyy-MM-dd
		 * yyyy/MM/dd
		 * yyyy.MM.dd
		 * yyyy-MM-dd HH:mm:ss
		 * yyyy/MM/dd HH:mm:ss
		 * yyyy-MM-dd HH:mm:ss SSS
		 * yyyy/MM/dd HH:mm:ss SSS
		 * @param {} s
		 * @return {Boolean}
		 */
		validate : function (s){
			var regex = /^(\d{2,4})([-/])(\d{1,2})([-/])(\d{1,2})( (\d{1}|[0-2][0-3])(:)(\d{1}|[0-5][0-9])(:)(\d{1}|[0-5][0-9])(( |\.)\d{1,3}){0,1}){0,1}$/;
			if (!regex.exec(s)){
				return false;
			} else {
				return true;
			}
		},
		
		/**
		 * 将字符串转换为日期对象
		 * @param {} s
		 * @return {}
		 */
		convert : function (s){
			var millisecondNum = '';
			s = s.replace(/-/g,'/');
			//因为parse方法只有到秒而已,所以需要针对有毫秒的再做一个特殊处理
			var p = /( |\.)(\d{1,3})$/g
			
			findArray = p.exec(s);
			if (!findArray){
				;
			} else {
				millisecondNum = findArray[0];
				s = s.replace(p,'');
			}
			var d = new Date(Date.parse(s));
			if (null != millisecondNum && '' != millisecondNum){
				d.setMilliseconds(millisecondNum);
			}
			
			return d;
		},
		
		/**
		 * 格式化日期对象
		 * y : 年
		 * M : 月
		 * d : 日
		 * h : 时
		 * m : 分
		 * s : 秒
		 * @param {} formatStr
		 * @param {} date
		 * @return {}
		 */
		format : function(formatStr,date){     
		    var str = formatStr;     
		    var Week = ['日','一','二','三','四','五','六'];    
		    
		    str=str.replace(/yyyy|YYYY/,date.getFullYear());     
		    str=str.replace(/yy|YY/,(date.getYear() % 100)>9?(date.getYear() % 100).toString():'0' + (date.getYear() % 100));     
		    
		    str=str.replace(/MM/,((date.getMonth()+1)>9)?((date.getMonth()+1).toString()):'0' + date.getMonth()+1);     
		    str=str.replace(/M/g,date.getMonth()+1);     
		    
		    str=str.replace(/w|W/g,Week[date.getDay()]);     
		    
		    str=str.replace(/dd|DD/,date.getDate()>9?date.getDate().toString():'0' + date.getDate());     
		    str=str.replace(/d|D/g,date.getDate());     
		    
		    str=str.replace(/hh|HH/,date.getHours()>9?date.getHours().toString():'0' + date.getHours());     
		    str=str.replace(/h|H/g,date.getHours());     
		    str=str.replace(/mm/,date.getMinutes()>9?date.getMinutes().toString():'0' + date.getMinutes());     
		    str=str.replace(/m/g,date.getMinutes());     
		    
		    str=str.replace(/ss|SS/,date.getSeconds()>9?date.getSeconds().toString():'0' + date.getSeconds());     
		    str=str.replace(/s|S/g,date.getSeconds());     
		    
		    return str;     
		},
		
		/**
		 * 获取两个日期之间相差多少天 不够一天也自动补齐到一天
		 * @param {} firstDate
		 * @param {} lastDate
		 * @return {}
		 */
		betweenDays : function(firstDate,lastDate){
			
			firstDate = this._checkType(firstDate);
			lastDate = this._checkType(lastDate);
			
			var n = (firstDate.getTime() - lastDate.getTime()) / 1000 / 60 / 60 / 24;
			
			return Math.ceil(n);
		},
		
		
		/**
		 * 比较两个日期的大小
		 * 
		 * 如果:
		 * firstDate > lastDate 返回 1
		 * firstDate = lastDate 返回 0
		 * firstDate < lastDate 返回 -1
		 * @param {} firstDate
		 * @param {} lastDate
		 */
		compare : function(firstDate,lastDate){
			firstDate = this._checkType(firstDate);
			lastDate = this._checkType(lastDate);
			
			if (firstDate.getTime() > lastDate.getTime()){
				return 1;
			} else if (firstDate.getTime() < lastDate.getTime()){
				return -1;
			} else {
				return 0;
			}
			
		},
		
		/**
		 * 获取年龄
		 * @param {} birthday
		 * @param {} currentDay
		 * @return {}
		 */
		getAge : function(birthday,currentDay){
			birthday = this._checkType(birthday);
			currentDay = this._checkType(currentDay);
			
			var age = currentDay.getFullYear() - birthday.getFullYear();
			if (age > 0){
				return age;
			} else if (age == 0){
				return 1;
			} else {
				alert('当前日期不能小于出生日期!');
				throw new Error('当前日期不能小于出生日期!');
			}
		}
	}
	
	

}
JS 判断字符串中某个特定字符出现的次数
function charCount(s,c){
	if (s){
		var count = (s.split(c)).length - 1;
		return count;
	} else {
		return 0;
	}
}
Global site tag (gtag.js) - Google Analytics