• 2668阅读
  • 1回复

js比较日期大小 [复制链接]

上一主题 下一主题
离线韭菜
 

只看楼主 倒序阅读 0楼 发表于: 2007-03-28
  1. //判断输入是否是有效的日期格式 "YYYY-MM-DD"
  2. function isdate(str){try{var result=str.match(/^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2})$/);if(result==null) return false;var d= new Date(result[1], result[3]-1, result[4]);return (d.getFullYear()==result[1]&&(d.getMonth()+1)==result[3]&&d.getDate()==result[4]);}catch (e){return false;}}
  3. //比较日期大小 // return true if str1 < str2
  4. function dateDiff(str1,str2){if (!isdate(str1)) return false;if (!isdate(str2)) return false;
  5. var date1 = str1.split('-'); var date2 = str2.split('-');var t1 = new Date(); var t2 = new Date();
  6. t1.setFullYear(date1[0],date1[1],date1[2]);t2.setFullYear(date2[0],date2[1],date2[2]);return t1<t2;
  7. }

离线韭菜

只看该作者 1楼 发表于: 2010-02-25
var s1 = "2007-12-01";
var s2 = "2007-1-31";

s1 = s1.replace(/-/g, "/");
s2 = s2.replace(/-/g, "/");
s1 = new Date(s1);
s2 = new Date(s2);

alert(s1.toLocaleString() + " " + s2.toLocaleString());


var days= s1.getTime() - s2.getTime();


alert( days>0 ? 's1>s2' : 's1<s2');
grant all privileges on *.* to 'a'@'localhost' identified by 'a' with grant option;flush privileges;
快速回复
限100 字节
 
上一个 下一个