js日期、天数计算方式

转载自:
计算两个日期相差天数:
转载自:http://www.jb51.net/article/44927.htm
function btnCount_Click(){
s1 = "2002-1-10"
s2 = "2002-10-1"
alert(DateDiff(s1,s2))
}

//计算天数差的函数,通用
function DateDiff(sDate1, sDate2){ //sDate1和sDate2是2002-12-18格式
var aDate, oDate1, oDate2, iDays
aDate = sDate1.split("-")
oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]) //转换为12-18-2002格式
aDate = sDate2.split("-")
oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24) //把相差的毫秒数转换为天数
return iDays
}

转载自:https://www.cnblogs.com/carekee/articles/1678041.html

Js获取当前日期时间及其它操作

var myDate = new Date();
myDate.getYear(); <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> //获取当前年份(2位)
myDate.getFullYear(); <wbr> <wbr> <wbr> //获取完整的年份(4位,1970-????)
myDate.getMonth(); <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> //获取当前月份(0-11,0代表1月)
myDate.getDate(); <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> //获取当前日(1-31)
myDate.getDay(); <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> //获取当前星期X(0-6,0代表星期天)
myDate.getTime(); <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours(); <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> //获取当前小时数(0-23)
myDate.getMinutes(); <wbr> <wbr> <wbr> <wbr> //获取当前分钟数(0-59)
myDate.getSeconds(); <wbr> <wbr> <wbr> <wbr> //获取当前秒数(0-59)
myDate.getMilliseconds(); <wbr> <wbr> <wbr> //获取当前毫秒数(0-999)
myDate.toLocaleDateString(); <wbr> <wbr> <wbr> <wbr> //获取当前日期
var mytime=myDate.toLocaleTimeString(); <wbr> <wbr> <wbr> <wbr> //获取当前时间
myDate.toLocaleString( ); <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> //获取日期与时间

<wbr>

<u>日期时间</u>脚本库方法列表

<u>Date</u>.prototype.isLeapYear 判断闰年
Date.prototype.Format 日期格式化
Date.prototype.DateAdd 日期计算
Date.prototype.DateDiff 比较日期差
Date.prototype.toString 日期转字符串
Date.prototype.toArray 日期分割为数组
Date.prototype.DatePart 取日期的部分信息
Date.prototype.MaxDayOfDate 取日期所在月的最大<u>天数</u>
Date.prototype.WeekNumOfYear 判断日期所在年的第几周
StringToDate 字符串转日期型
IsValidDate 验证日期有效性
CheckDateTime 完整日期时间检查
daysBetween 日期天数差

js代码:

//--------------------------------------------------- <wbr> <wbr>
// 判断闰年 <wbr> <wbr>
//--------------------------------------------------- <wbr> <wbr>
Date.prototype.isLeapYear = function() <wbr> <wbr> <wbr>
{ <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> return (0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0))); <wbr> <wbr> <wbr>
} <wbr> <wbr> <wbr>
<wbr> <wbr>
//--------------------------------------------------- <wbr> <wbr>
// 日期格式化 <wbr> <wbr>
// 格式 YYYY/yyyy/YY/yy 表示年份 <wbr> <wbr>
// MM/M 月份 <wbr> <wbr>
// W/w 星期 <wbr> <wbr>
// dd/DD/d/D 日期 <wbr> <wbr>
// hh/HH/h/H 时间 <wbr> <wbr>
// mm/m 分钟 <wbr> <wbr>
// ss/SS/s/S 秒 <wbr> <wbr>
//--------------------------------------------------- <wbr> <wbr>
Date.prototype.Format = function(formatStr) <wbr> <wbr> <wbr>
{ <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> var str = formatStr; <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> var Week = ['日','一','二','三','四','五','六']; <wbr> <wbr>
<wbr> <wbr>
<wbr> <wbr> <wbr> str=str.replace(/yyyy|YYYY/,this.getFullYear()); <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100)); <wbr> <wbr> <wbr>
<wbr> <wbr>
<wbr> <wbr> <wbr> str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + this.getMonth()); <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> str=str.replace(/M/g,this.getMonth()); <wbr> <wbr> <wbr>
<wbr> <wbr>
<wbr> <wbr> <wbr> str=str.replace(/w|W/g,Week[this.getDay()]); <wbr> <wbr> <wbr>
<wbr> <wbr>
<wbr> <wbr> <wbr> str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate()); <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> str=str.replace(/d|D/g,this.getDate()); <wbr> <wbr> <wbr>
<wbr> <wbr>
<wbr> <wbr> <wbr> str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours()); <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> str=str.replace(/h|H/g,this.getHours()); <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes()); <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> str=str.replace(/m/g,this.getMinutes()); <wbr> <wbr> <wbr>
<wbr> <wbr>
<wbr> <wbr> <wbr> str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds()); <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> str=str.replace(/s|S/g,this.getSeconds()); <wbr> <wbr> <wbr>
<wbr> <wbr>
<wbr> <wbr> <wbr> return str; <wbr> <wbr> <wbr>
} <wbr> <wbr> <wbr>
<wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
//| 求两个时间的天数差 日期格式为 YYYY-MM-dd <wbr> <wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
function daysBetween(DateOne,DateTwo) <wbr> <wbr>
{ <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-')); <wbr> <wbr>
<wbr> <wbr> <wbr> var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1); <wbr> <wbr>
<wbr> <wbr> <wbr> var OneYear = DateOne.substring(0,DateOne.indexOf ('-')); <wbr> <wbr>
<wbr> <wbr>
<wbr> <wbr> <wbr> var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-')); <wbr> <wbr>
<wbr> <wbr> <wbr> var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1); <wbr> <wbr>
<wbr> <wbr> <wbr> var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-')); <wbr> <wbr>
<wbr> <wbr>
<wbr> <wbr> <wbr> var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000); <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> return Math.abs(cha); <wbr> <wbr>
} <wbr> <wbr>
<wbr> <wbr>
<wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
//| 日期计算 <wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
Date.prototype.DateAdd = function(strInterval, Number) { <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> var dtTmp = this; <wbr> <wbr>
<wbr> <wbr> <wbr> switch (strInterval) { <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 's' :return new Date(Date.parse(dtTmp) + (1000 * Number)); <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 'n' :return new Date(Date.parse(dtTmp) + (60000 * Number)); <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 'h' :return new Date(Date.parse(dtTmp) + (3600000 * Number)); <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 'd' :return new Date(Date.parse(dtTmp) + (86400000 * Number)); <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 'w' :return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number)); <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 'q' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 'm' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 'y' :return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); <wbr> <wbr>
<wbr> <wbr> <wbr> } <wbr> <wbr>
} <wbr> <wbr>
<wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
//| 比较日期差 dtEnd 格式为日期型或者有效日期格式字符串 <wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
Date.prototype.DateDiff = function(strInterval, dtEnd) { <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> var dtStart = this; <wbr> <wbr>
<wbr> <wbr> <wbr> if (typeof dtEnd == 'string' )//如果是字符串转换为日期型 <wbr> <wbr>
<wbr> <wbr> <wbr> { <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> dtEnd = StringToDate(dtEnd); <wbr> <wbr>
<wbr> <wbr> <wbr> } <wbr> <wbr>
<wbr> <wbr> <wbr> switch (strInterval) { <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 's' :return parseInt((dtEnd - dtStart) / 1000); <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 'n' :return parseInt((dtEnd - dtStart) / 60000); <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 'h' :return parseInt((dtEnd - dtStart) / 3600000); <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 'd' :return parseInt((dtEnd - dtStart) / 86400000); <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 'w' :return parseInt((dtEnd - dtStart) / (86400000 * 7)); <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 'm' :return (dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())
12) - (dtStart.getMonth()+1); <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 'y' :return dtEnd.getFullYear() - dtStart.getFullYear(); <wbr> <wbr>
<wbr> <wbr> <wbr> } <wbr> <wbr>
} <wbr> <wbr>
<wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
//| 日期输出字符串,重载了系统的toString方法 <wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
Date.prototype.toString = function(showWeek) <wbr> <wbr>
{ <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> var myDate= this; <wbr> <wbr>
<wbr> <wbr> <wbr> var str = myDate.toLocaleDateString(); <wbr> <wbr>
<wbr> <wbr> <wbr> if (showWeek) <wbr> <wbr>
<wbr> <wbr> <wbr> { <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> var Week = ['日','一','二','三','四','五','六']; <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> str += ' 星期' + Week[myDate.getDay()]; <wbr> <wbr>
<wbr> <wbr> <wbr> } <wbr> <wbr>
<wbr> <wbr> <wbr> return str; <wbr> <wbr>
} <wbr> <wbr>
<wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
//| 日期合法性验证 <wbr> <wbr>
//| 格式为:YYYY-MM-DD或YYYY/MM/DD <wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
function IsValidDate(DateStr) <wbr> <wbr> <wbr>
{ <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> var sDate=DateStr.replace(/(^\s+|\s+$)/g,''); //去两边空格; <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> if(sDate=='') return true; <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> //如果格式满足YYYY-(/)MM-(/)DD或YYYY-(/)M-(/)DD或YYYY-(/)M-(/)D或YYYY-(/)MM-(/)D就替换为'' <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> //数据库中,合法日期可以是:YYYY-MM/DD(2003-3/21),数据库会自动转换为YYYY-MM-DD格式 <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> var s = sDate.replace(/[\d]{ 4,4 }[-/]{ 1 }[\d]{ 1,2 }[-/]{ 1 }[\d]{ 1,2 }/g,''); <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> if (s=='') //说明格式满足YYYY-MM-DD或YYYY-M-DD或YYYY-M-D或YYYY-MM-D <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> { <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> var t=new Date(sDate.replace(/-/g,'/')); <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> var ar = sDate.split(/[-/:]/); <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if(ar[0] != t.getYear() || ar[1] != t.getMonth()+1 || ar[2] != t.getDate()) <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> { <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> //alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。'); <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> return false; <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> } <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> } <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> else <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> { <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> //alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。'); <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> return false; <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> } <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> return true; <wbr> <wbr> <wbr>
} <wbr> <wbr> <wbr>
<wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
//| 日期时间检查 <wbr> <wbr>
//| 格式为:YYYY-MM-DD HH:MM:SS <wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
function CheckDateTime(str) <wbr> <wbr>
{ <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> var reg = /^(\d+)-(\d{ 1,2 })-(\d{ 1,2 }) (\d{ 1,2 }):(\d{ 1,2 }):(\d{ 1,2 })$/; <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> var r = str.match(reg); <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> if(r==null)return false; <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> r[2]=r[2]-1; <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> var d= new Date(r[1],r[2],r[3],r[4],r[5],r[6]); <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> if(d.getFullYear()!=r[1])return false; <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> if(d.getMonth()!=r[2])return false; <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> if(d.getDate()!=r[3])return false; <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> if(d.getHours()!=r[4])return false; <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> if(d.getMinutes()!=r[5])return false; <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> if(d.getSeconds()!=r[6])return false; <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> return true; <wbr> <wbr> <wbr>
} <wbr> <wbr> <wbr>
<wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
//| 把日期分割成数组 <wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
Date.prototype.toArray = function() <wbr> <wbr>
{ <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> var myDate = this; <wbr> <wbr>
<wbr> <wbr> <wbr> var myArray = Array(); <wbr> <wbr>
<wbr> <wbr> <wbr> myArray[0] = myDate.getFullYear(); <wbr> <wbr>
<wbr> <wbr> <wbr> myArray[1] = myDate.getMonth(); <wbr> <wbr>
<wbr> <wbr> <wbr> myArray[2] = myDate.getDate(); <wbr> <wbr>
<wbr> <wbr> <wbr> myArray[3] = myDate.getHours(); <wbr> <wbr>
<wbr> <wbr> <wbr> myArray[4] = myDate.getMinutes(); <wbr> <wbr>
<wbr> <wbr> <wbr> myArray[5] = myDate.getSeconds(); <wbr> <wbr>
<wbr> <wbr> <wbr> return myArray; <wbr> <wbr>
} <wbr> <wbr>
<wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
//| 取得日期数据信息 <wbr> <wbr>
//| 参数 interval 表示数据类型 <wbr> <wbr>
//| y 年 m月 d日 w星期 ww周 h时 n分 s秒 <wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
Date.prototype.DatePart = function(interval) <wbr> <wbr>
{ <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> var myDate = this; <wbr> <wbr>
<wbr> <wbr> <wbr> var partStr=''; <wbr> <wbr>
<wbr> <wbr> <wbr> var Week = ['日','一','二','三','四','五','六']; <wbr> <wbr>
<wbr> <wbr> <wbr> switch (interval) <wbr> <wbr>
<wbr> <wbr> <wbr> { <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 'y' :partStr = myDate.getFullYear();break; <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 'm' :partStr = myDate.getMonth()+1;break; <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 'd' :partStr = myDate.getDate();break; <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 'w' :partStr = Week[myDate.getDay()];break; <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 'ww' :partStr = myDate.WeekNumOfYear();break; <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 'h' :partStr = myDate.getHours();break; <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 'n' :partStr = myDate.getMinutes();break; <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> case 's' :partStr = myDate.getSeconds();break; <wbr> <wbr>
<wbr> <wbr> <wbr> } <wbr> <wbr>
<wbr> <wbr> <wbr> return partStr; <wbr> <wbr>
} <wbr> <wbr>
<wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
//| 取得当前日期所在月的最大天数 <wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
Date.prototype.MaxDayOfDate = function() <wbr> <wbr>
{ <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> var myDate = this; <wbr> <wbr>
<wbr> <wbr> <wbr> var ary = myDate.toArray(); <wbr> <wbr>
<wbr> <wbr> <wbr> var date1 = (new Date(ary[0],ary[1]+1,1)); <wbr> <wbr>
<wbr> <wbr> <wbr> var date2 = date1.dateAdd(1,'m',1); <wbr> <wbr>
<wbr> <wbr> <wbr> var result = dateDiff(date1.Format('yyyy-MM-dd'),date2.Format('yyyy-MM-dd')); <wbr> <wbr>
<wbr> <wbr> <wbr> return result; <wbr> <wbr>
} <wbr> <wbr>
<wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
//| 取得当前日期所在周是一年中的第几周 <wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
Date.prototype.WeekNumOfYear = function() <wbr> <wbr>
{ <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> var myDate = this; <wbr> <wbr>
<wbr> <wbr> <wbr> var ary = myDate.toArray(); <wbr> <wbr>
<wbr> <wbr> <wbr> var year = ary[0]; <wbr> <wbr>
<wbr> <wbr> <wbr> var month = ary[1]+1; <wbr> <wbr>
<wbr> <wbr> <wbr> var day = ary[2]; <wbr> <wbr>
<wbr> <wbr> <wbr> document.write('< script language=VBScript> \n'); <wbr> <wbr>
<wbr> <wbr> <wbr> document.write('myDate = Datue(''+month+'-'+day+'-'+year+'') \n'); <wbr> <wbr>
<wbr> <wbr> <wbr> document.write('result = DatePart('ww', myDate) \n'); <wbr> <wbr>
<wbr> <wbr> <wbr> document.write(' \n'); <wbr> <wbr>
<wbr> <wbr> <wbr> return result; <wbr> <wbr>
} <wbr> <wbr>
<wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
//| 字符串转成日期类型 <wbr> <wbr> <wbr>
//| 格式 MM/dd/YYYY MM-dd-YYYY YYYY/MM/dd YYYY-MM-dd <wbr> <wbr>
//+--------------------------------------------------- <wbr> <wbr>
function StringToDate(DateStr) <wbr> <wbr>
{ <wbr> <wbr> <wbr>
<wbr> <wbr>
<wbr> <wbr> <wbr> var converted = Date.parse(DateStr); <wbr> <wbr>
<wbr> <wbr> <wbr> var myDate = new Date(converted); <wbr> <wbr>
<wbr> <wbr> <wbr> if (isNaN(myDate)) <wbr> <wbr>
<wbr> <wbr> <wbr> { <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> //var delimCahar = DateStr.indexOf('/')!=-1?'/':'-'; <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> var arys= DateStr.split('-'); <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> myDate = new Date(arys[0],--arys[1],arys[2]); <wbr> <wbr>
<wbr> <wbr> <wbr> } <wbr> <wbr>
<wbr> <wbr> <wbr> return myDate; <wbr> <wbr>
} <wbr> <wbr>

<wbr>

若要显示:当前日期加时间(如:2009-06-12 12:00)

function CurentTime()
<wbr> <wbr> <wbr> { <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> var now = new Date();
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> var year = now.getFullYear(); <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> //年
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> var month = now.getMonth() + 1; <wbr> <wbr> <wbr> <wbr> //月
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> var day = now.getDate(); <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> //日
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> var hh = now.getHours(); <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> //时
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> var mm = now.getMinutes(); <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> //分
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> var clock = year + "-";
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if(month < 10)
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> clock += "0";
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> clock += month + "-";
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if(day < 10)
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> clock += "0";
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> clock += day + " ";
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if(hh < 10)
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> clock += "0";
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> clock += hh + ":";
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if (mm < 10) clock += '0'; <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> clock += mm; <wbr>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> return(clock); <wbr>
<wbr> <wbr> <wbr> }

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,222评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,455评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,720评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,568评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,696评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,879评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,028评论 3 409
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,773评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,220评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,550评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,697评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,360评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,002评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,782评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,010评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,433评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,587评论 2 350

推荐阅读更多精彩内容