日期:2014-05-16  浏览次数:20638 次

js 判断日期是否是当天

/**
??? ??? ??? ??? 如果是当日的话显示hh:mm;
??? ??? ??? ??? 不是当日的话显示MM-dd;
??? ??? ??? ??? 传入的时间格式为'yyyy-MM-dd hh:mm:ss | yyyy-MM-dd hh:mm'
??? ??? ??? ??? **/

var reg = /^(\d{4})-(\d{1,2})-(\d{1,2}) (\d{2}):(\d{2})(?::\d{1,2})?$/;
??? ??? ??? ??? var standard = new Date();
??? ??? ??? ??? var getSentMessageTime = function(other){
??? ??? ??? ??? ??? // 通过正则得到["2010-02-25 12:23:01", "2010", "02", "25", "12", "23"]
??? ??? ??? ??? ??? var times =other.match(reg) ;??
??? ??? ??? ??? ??? if(isToDay(times[1],times[2],times[3])){
??? ??? ??? ??? ??? ??? return times[4]+":"+times[5];
??? ??? ??? ??? ??? }else return times[2]+"-"+times[3];??? ??? ??? ??? ???
??? ??? ??? ??? };??? ??? ???
??? ??? ??? ??? function isToDay(year,month,day){
??? ??? ??? ??? ??? return (parseInt(year) == standard.getFullYear()) && (parseInt(month) == standard.getMonth()+1) && (parseInt(day) == standard.getDate());
??? ??? ??? ??? };
??? ??? ??? ???
??? ??? ??? ??? var getSentTimeByMill = function(other){
??? ??? ??? ??? ??? var time = new Date(other);??? ??? ??? ??? ??? ??? ??? ??? ??? ???
??? ??? ??? ??? ??? ??? var otherMonth = time.getMonth()+1;
??? ??? ??? ??? ??? ??? otherMonth = otherMonth>9?otherMonth:'0'+otherMonth;
??? ??? ??? ??? ??? ??? var otherDay = time.getDate();
??? ??? ??? ??? ??? ??? otherDay = otherDay>9?otherDay:'0'+otherDay;
??? ??? ??? ??? ??? ??? var otherYear = time.getFullYear();
??? ??? ??? ??? ??? ???
??? ??? ??? ??? ??? ??? var otherDate = otherMonth+'-'+otherDay;???
??? ??? ??? ??? ??? ??? var hours = time.getHours();
??? ??? ??? ??? ??? ??? var minu = time.getMinutes()
??? ??? ??? ??? ??? ??? var otherTime =(hours>9?hours:'0'+hours) +":"+ (minu>9?minu:'0'+minu);
??? ??? ??? ??? ??? ??? if(isToDay(otherYear,otherMonth,otherDay)){
??? ??? ??? ??? ??? ??? ??? return otherTime;
??? ??? ??? ??? ??? ??? }else return otherDate;??? ???
??? ??? ??? ??? };