function FormatDateTime(DateStr,FormatStr)
{
	document.write(StringToDate(DateStr).Format(FormatStr));
}
//---------------------------------------------------
// 字符串转成日期类型
// 格式为：YYYY-MM-DD HH:MM:SS
//---------------------------------------------------
function StringToDate(DateStr)
{
	var myDate=DateStr;
	if(IsValidDate(myDate))
	{
		myDate=myDate+" 00:00:00";
	}
	if(CheckDateTime(myDate))
	{
		var ary=myDate.split(' ');
		var aryd=ary[0].split('-');
		var aryt=ary[1].split(':');
		myDate=new Date(aryd[0],--aryd[1],aryd[2],aryt[0],aryt[1],aryt[2]);
	}
	if (isNaN(myDate))
	{
		myDate=new Date();
	}
	return myDate;
}
//---------------------------------------------------
// 日期格式化
// 格式 YYYY/yyyy/YY/yy 表示年份
// MM|M 月份
// W/w 星期
// dd/DD/d/D 日期
// hh/HH/h/H 时间
// mm/m 分钟
// ss/SS/s/S 秒
//---------------------------------------------------
Date.prototype.Format=function(formatStr)
{
	var str=formatStr;
	var Week=['日','一','二','三','四','五','六'];
	str=str.replace(/yyyy|YYYY/,this.getFullYear());
	str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));
	str=str.replace(/MM/,this.getMonth()>9?eval(this.getMonth()+1).toString():'0' + eval(this.getMonth()+1));
	str=str.replace(/M/g,eval(this.getMonth()+1));
	str=str.replace(/w|W/g,Week[this.getDay()]);
	str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());
	str=str.replace(/d|D/g,this.getDate());
	str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());
	str=str.replace(/h|H/g,this.getHours());
	str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());
	str=str.replace(/m/g,this.getMinutes());
	str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());
	str=str.replace(/s|S/g,this.getSeconds());
	return str;
}
//---------------------------------------------------
// 日期合法性验证
// 格式为：YYYY-MM-DD或YYYY/MM/DD
//---------------------------------------------------
function IsValidDate(DateStr)
{
	var sDate=DateStr.replace(/(^\s+|\s+$)/g,''); //去两边空格;
	if(sDate=='') return true;
	//如果格式满足YYYY-(/)MM-(/)DD或YYYY-(/)M-(/)DD或YYYY-(/)M-(/)D或YYYY-(/)MM-(/)D就替换为''
	//数据库中，合法日期可以是:YYYY-MM/DD(2003-3/21),数据库会自动转换为YYYY-MM-DD格式
	var s=sDate.replace(/[\d]{4,4}[\-/]{1}[\d]{1,2}[\-/]{1}[\d]{1,2}/g,'');
	if (s=='') //说明格式满足YYYY-MM-DD或YYYY-M-DD或YYYY-M-D或YYYY-MM-D
	{
		var t=new Date(sDate.replace(/\-/g,'/'));
		var ar=sDate.split(/[-/:]/);
		if(ar[0] != t.getYear() || ar[1] != t.getMonth()+1 || ar[2] != t.getDate())
		{
			//alert('错误的日期格式！格式为：YYYY-MM-DD或YYYY/MM/DD。注意闰年。');
			return false;
		}
	}
	else
	{
		//alert('错误的日期格式！格式为：YYYY-MM-DD或YYYY/MM/DD。注意闰年。');
		return false;
	}
	return true;
}
//---------------------------------------------------
//| 日期时间检查
//| 格式为：YYYY-MM-DD HH:MM:SS
//---------------------------------------------------
function CheckDateTime(str)
{
	var reg=/^(\d+)-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
	var r=str.match(reg);
	if(r==null)return false;
	r[2]=r[2]-1;
	var d= new Date(r[1],r[2],r[3],r[4],r[5],r[6]);
	if(d.getFullYear()!=r[1])return false;
	if(d.getMonth()!=r[2])return false;
	if(d.getDate()!=r[3])return false;
	if(d.getHours()!=r[4])return false;
	if(d.getMinutes()!=r[5])return false;
	if(d.getSeconds()!=r[6])return false;
	return true;
}
