首页 > 代码库 > JQuery时间格式化插件--扩展JQuery

JQuery时间格式化插件--扩展JQuery


(function($){
	$.formatDate = function(pattern,date){
		//如果不设置,默认为当前时间
		if(!date) date = new Date();
		if(typeof(date) ==="string"){
			 if(date=="")  date = new Date();
			  else  date = new Date(date.replace(/-/g,"/"));
		}	
		/*补00*/
		var toFixedWidth = function(value){
			 var result = 100+value;
			 return result.toString().substring(1);
		};
		
		/*配置*/
		var options = {
				regeExp:/(yyyy|M+|d+|h+|m+|s+|ee+|ws?|p)/g,
				months: ['January','February','March','April','May',
				         'June','July', 'August','September',
						  'October','November','December'],
				weeks: ['Sunday','Monday','Tuesday',
				        'Wednesday','Thursday','Friday',
							'Saturday']
		};
		
		/*时间切换*/
		var swithHours = function(hours){
			return hours<12?"AM":"PM";
		};
		
		/*配置值*/
		var pattrnValue = http://www.mamicode.com/{>

调用代码:

$(function($){
	 var time = new Date("2014-01-01 13:20:30");
	  console.info($.formatDate("yyyy-MM-dd hh:mm:ss ws ee","2013-1-1 13:20:30"));
          alert($.formatDate("yyyy-MM-dd hh:mm:ss ws ee",time));
});