天天看點

js字元串轉為日期比較大小

demo1:dd-MM-yyyy類型的日期大小比較1:

<script>
function dateCompare(strDate1,strDate2){
	var date1 = stringToDate(strDate1);
	var date2 = stringToDate(strDate2);
	var result = Date.parse(date1) - Date.parse(date2);
	return result;
}

function stringToDate(dateStr){ 
    var newDateStr = dateStr.substr(6,4)+"-"+dateStr.substr(3,2)+"-"+dateStr.substr(0,2); 
	var date = new Date(newDateStr);
	if (isNaN(newDateStr)){           
        var arys= newDateStr.split('-');     
        date = new Date(arys[0], arys[1] - 1, arys[2]);      
    }
	return date;
}
</script>      

demo2:日期大小比較2

<html>
	<head>
		<script language="javascript" type="text/javascript">
			/** 日期比較 **/
			function compareDate(strDate1,strDate2)
			{
				var date1 = new Date(strDate1.replace(/\-/g, "\/"));
				var date2 = new Date(strDate2.replace(/\-/g, "\/"));
				return date1-date2;
			}
			
			/** 比較 **/
			function doCompare(){
				var strDate1 = document.getElementById("strDate1").value;
				var strDate2 = document.getElementById("strDate2").value;
				var result = compareDate(strDate1,strDate2);
				if ( result>0 ) {
					alert("strDate1晚于strDate2");
				}else if( result<0 ){
					alert("strDate1早于strDate2");
				}else if ( result==0 ){
					alert("strDate1等于strDate2");
				}
			}
		</script>
	</head>
	<body>
		<input type="text" id="strDate1" name="strDate1" value="2012-07-01"/>
		<input type="text" id="strDate2" name="strDate2" value="2012-08-01"/>
		<input type="button" id="compareBtn" name="compareBtn" value="比較" onClick="doCompare();"/>
	</body>
</html>
           

demo3:格式化日期為字元串

<script language="JavaScript">
/**
 * 格式化日期
 * 格式 yyyy-MM-dd hh:mm:ss
 */
Date.prototype.format = function(format) {
	var o = {
		"M+" : this.getMonth() + 1, //month
		"d+" : this.getDate(), //day
		"h+" : this.getHours(), //hour
		"m+" : this.getMinutes(), //minute
		"s+" : this.getSeconds(), //second
		"q+" : Math.floor((this.getMonth() + 3) / 3), //quarter
		"S" : this.getMilliseconds()
	//millisecond
	}
	if (/(y+)/.test(format))
		format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for ( var k in o) {
		if (new RegExp("(" + k + ")").test(format))
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
	}
	return format;
}

// 調用方法:
var time1 = new Date().format("yyyy-MM-dd"); 
alert(time1);

</script>      

demo4:日期的特殊處理

<script language="JavaScript">
/**
 * 加上天數傳回新的日期
 * iDay 天數
 */
Date.prototype.addDay = function(iDay) {
	var d = new Date(this);
	d.setDate(d.getDate() + iDay);
	return d;
}
/**
 * 加上月數傳回新的日期
 * iMonth 月數
 */
Date.prototype.addMonth = function(iMonth) {
	var d = new Date(this);
	d.setMonth(d.getMonth() + iMonth);
	return d;
}
/**
 * 獲得周的第一天日期
 */
Date.prototype.getWeekFirstDate = function() {
	//中國周的第一天是周一
	//return this.addDay(-this.getDay() + 1);
	//周的第一天是周日
	return this.addDay(-this.getDay());
}
/**
 * 獲得周的最後一天日期
 */
Date.prototype.getWeekLastDate = function() {
	//中國周的最後一天是周日
	//return this.addDay(7 - this.getDay());
	//周的最後一天是周六
	return this.addDay(6 - this.getDay());
}
/**
 * 獲得月的第一天日期
 */
Date.prototype.getMonthFirstDate = function() {
	return new Date(this.getYear(), this.getMonth(), 1);
}
/**
 * 獲得月的最後一天日期
 */
Date.prototype.getMonthLastDate = function() {
	return new Date(this.getYear(), this.getMonth() + 1, 0);
}
/**
 * 獲得星期幾
 */
Date.prototype.getWeekStr = function() {
	var weekNumber = [ "日", "一", "二", "三", "四", "五", "六" ];
	return "星期" + weekNumber[this.getDay()];
}
function test() {
	alert("今天加一天是:" + new Date().addDay(1).format("yyyy-MM-dd hh:mm:ss"));
	alert("今天加一個月是:" + new Date().addMonth(1).format("yyyy-MM-dd hh:mm:ss"));
	alert("今天是" + new Date().getWeekStr());
	alert("目前周第一天是 : " + new Date().getWeekFirstDate().format("yyyy-MM-dd"));
	alert("目前周最後一天是 : " + new Date().getWeekLastDate().format("yyyy-MM-dd"));
	alert("目前月第一天是 : " + new Date().getMonthFirstDate().format("yyyy-MM-dd"));
	alert("目前月最後一天是 : " + new Date().getMonthLastDate().format("yyyy-MM-dd"));
}
test();
</script>