var myDate=new date();//创建一个日期对象
myDate.getYear(); //获取当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-????)
myDate.getMonth(); //获取当前月份(0-11,0代表1月)
myDate.getDate(); //获取当前日(1-31)
myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours(); //获取当前小时数(0-23)
myDate.getMinutes(); //获取当前分钟数(0-59)
myDate.getSeconds(); //获取当前秒数(0-59)
myDate.getMilliseconds(); //获取当前毫秒数(0-999)
myDate.toLocaleDateString(); //获取当前日期
var mytime=myDate.toLocaleTimeString(); //获取当前时间
myDate.toLocaleString( ); //获取日期与时间
计算两个时间之差
var start = new Date(2010,1,1);
var end = new Date(2010,4,28);
alert(parseInt((end-start) / (1000 * 60 * 60 * 24))); 如果是负值,表示end比start小
//格式化时间
Date.prototype.format = function(format) //author: meizz
{ 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;
}
//判断发生时间要大于出生日期
function checkHappentimeBigBir(name,obj)
{
var birth=document.getElementById("DCsrq").innerHTML;
if(birth==""){ return true; }
ymd1=birth.split("-");
month1=ymd1[1]-1;
var Date1 = new Date(ymd1[0],month1,ymd1[2]);
if(obj.value=="") { return true; } ymd2=obj.value.split("-");
month2=ymd2[1]-1;
var Date2 = new Date(ymd2[0],month2,ymd2[2]);
if(Date1.format('yyyy-MM-dd')>Date2.format('yyyy-MM-dd')){ alert(name+"不能小于出生时间!");
obj.value="";
obj.focus();
return false;
}
}
//调用该方法(主方法)
function dateDiff(date1, date2){
var type1 = typeof date1, type2 = typeof date2;
if(type1 == 'string') date1 = stringToTime(date1);
else if(date1.getTime) date1 = date1.getTime();
if(type2 == 'string') date2 = stringToTime(date2);
else if(date2.getTime) date2 = date2.getTime();
return (date1 - date2) / 1000;//除1000是毫秒,不加是秒
}
//字符串转成Time(dateDiff)所需方法
function stringToTime(string){
var f = string.split(' ', 2);
var d = (f[0] ? f[0] : '').split('-', 3);
var t = (f[1] ? f[1] : '').split(':', 3);
return (new Date( parseInt(d[0], 10) || null, (parseInt(d[1], 10) || 1)-1, parseInt(d[2], 10) || null, parseInt(t[0], 10) || null, parseInt(t[1], 10) || null, parseInt(t[2], 10) || null )).getTime();
}
//调用 dateDiff("2009-10-10 19:00:00","2009-10-10 18:00:00")
var endDate = Date.parse("4-28-2010");
var Timer=new Date(); alert(1);
var hours=Timer.getHours();
var minutes=Timer.getMinutes();
var seconds=Timer.getSeconds();
var noon="AM";
if (hours>12){
noon="PM";
hours=hours-12;
}
if (hours==0) hours=12;
if (minutes<=9) minutes="0"+minutes;
if (seconds<=9) seconds="0"+seconds;
myclock=hours+":"+minutes+":"+seconds+" "+noon+";
alert(myclock);
http://www.cainiao8.com/web/js_note/js_duixiang/zifuchuan_07_split.html 菜鸟吧比较不,可以看看