Last active
March 2, 2016 14:20
-
-
Save xie-qianyue/92a45ea886e53f13ee1d to your computer and use it in GitHub Desktop.
JS Utilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Transform the date format to String format | |
var Date = new Date(); | |
var str_GMT = date.toGMTString(); // 把Date对象的日期(一个数值)转变成一个GMT时间字符串,返回类似下面的值:Weds,14 May 2007 10:02:02 GMT(精确的格式依赖于计算机上所运行的操作系统而变) | |
var str_Locale = date.toLocaleString(); // 把Date对象的日期(一个数值)转变成一个字符串,使用所在计算机上配置使用的特定日期格式 UTC() 使用Date UTC(年、月、日、时、分、秒),以自从1997年1月1日00:00:00(其中时、分、秒是可选的)以来的毫秒数的形式返回日期 | |
// Get the interval of two days | |
alert("间隔天数为:"+(new Date('2005/8/15')-new Date('2003/9/18'))/1000/60/60/24+"天"); | |
// Get the detail interval of two days | |
var d1=new Date("2004/09/16 20:08:00"); | |
var d2=new Date("2004/09/16 10:18:03"); | |
var d3=d1-d2; | |
var h=Math.floor(d3/3600000); | |
var m=Math.floor((d3-h*3600000)/60000); | |
var s=(d3-h*3600000-m*60000)/1000; | |
alert("相差"+h+"小时"+m+"分"+s+"秒"); | |
// Validation | |
function CheckDate(strDate){ | |
var strDate = document.getElementById("date_hour").value; | |
var reg=/^(\d{4})([\/])(\d{2})([\/])(\d{2})$/; | |
if(!reg.test(strDate)){ | |
alert("日期格式不正确!/n正确格式为:2004/01/01"); | |
return false; | |
} | |
var ss=strDate.split("/"); | |
var year=ss[0]; | |
var month=ss[1]; | |
var date=ss[2]; | |
if(!checkYear(year)){ | |
return false; | |
} | |
if(!checkMonth(month)){ | |
return false; | |
} | |
if(!checkDate(year,month,date)){ | |
return false; | |
} | |
return true; | |
} | |
function checkYear(year){ | |
if(isNaN(parseInt(year))){ | |
alert("年份输入有误,请重新输入!"); | |
return false; | |
}else if(parseInt(year)<1950 || parseInt(year) >2050{ | |
alert("年份应该在1950-2050之间!"); | |
return false; | |
}else{ | |
return true; | |
} | |
} | |
function checkMonth(month){ | |
if(isNaN(parseInt(month,10))){ | |
alert("月份输入有误,请重新输入!"); | |
return false; | |
}else if(parseInt(month,10)<1 || parseInt(month,10) >12){ | |
alert("月份应该在1-12之间!"); | |
return false; | |
}else { | |
return true; | |
} | |
} | |
function checkDate(year,month,date){ | |
var daysOfMonth=CalDays(parseInt(year),parseInt(month)); | |
if(isNaN(parseInt(date))){ | |
alert("日期输入有误,请重新输入!"); | |
return false; | |
}else if(parseInt(date)<1||parseInt(date)>daysOfMonth){ | |
alert("日期应该在1-"+daysOfMonth+"之间!"); | |
return false; | |
}else{ | |
return true; | |
} | |
} | |
function CalDays(year,month){ | |
var date= new Date(year,month,0); | |
return date.getDate(); | |
} | |
function isLeapYear(year){ | |
if((year %4==0 && year %100!=0) || (year %400==0)){ | |
return true; | |
} else { | |
return false; | |
} | |
} | |
var isDate = function(date) { | |
return (new Date(date) !== "Invalid Date" && !isNaN(new Date(date)) ) ) ? true : false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var isIE6 = (navigator.appVersion.toLowerCase().indexOf('msie 6') > 0); | |
var isNotIE7 = (navigator.appVersion.toLowerCase().indexOf('msie 7') == -1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Transformationi Number to String | |
str = parseInt(str, 10); // 10 as the radix sets parseInt to expect decimals | |
str = new Number(str); // this does not support octals | |
str = +str; // the + operator causes the parser to convert using Number |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment