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>
本文提供了多种日期操作的方法,包括不同格式日期的比较、日期格式化、日期增减等实用功能,并展示了具体的JavaScript实现。
193

被折叠的 条评论
为什么被折叠?



