Javascript Trim Stand-Alone Functions
If you prefer not to modify the string prototype, then you can use the stand-alone functions below.
function trim(stringToTrim) {
return stringToTrim.replace(/^/s+|/s+$/g,"");
}
function ltrim(stringToTrim) {
return stringToTrim.replace(/^/s+/,"");
}
function rtrim(stringToTrim) {
return stringToTrim.replace(//s+$/,"");
}
// example of using trim, ltrim, and rtrim
var myString = " hello my name is ";
alert("*"+trim(myString)+"*");
alert("*"+ltrim(myString)+"*");
alert("*"+rtrim(myString)+"*");
JS (Javascript)取当前日期、时间的代码
<SCRIPT LANGUAGE="JavaScript">
<!--
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( ); //获取日期与时间
if (mytime<"23:30:00")
{
alert(mytime);
}
//-->
</SCRIPT>
//Javscript正则表达式验证表达
//01/31/2008 22:03
function DateFormatCheck(obj)
{
var rr=/^(?:(?:0[1-9])|(?:1[0-2]))//(?:(?:[0-2][1-9])|(?:[1-3][0-1]))//(?:19|20)[0-9][0-9] (?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]$/
obj.value = trim(obj.value);
if(!rr.test(obj.value))
{
alert('Please Check The Format,Example:01/27/2008 22:12');
obj.focus();
return false;
}
if (!isDate(obj.value))
{
alert('The Date Is Wrong !');
obj.focus();
return false;
}
return true;
}
function isDate(sDate)
{
sDate = sDate.toString();
var monthOfDate = sDate.substring(0,2);
var dayOfDate = sDate.substring(3,5);
var yearOfDate = sDate.substring(6,10);
sDate = (sDate.substring(0,10));
var tempDate = new Date(Date.parse(sDate));
var tempYear = tempDate.getYear();
var tempMonth = tempDate.getMonth() + 1;
var tempDate = tempDate.getDate();
if(yearOfDate == tempYear && monthOfDate == tempMonth && dayOfDate == tempDate)
{
return true;
}else{
return false;
}
}
控件调用:
<td>
<input type="text" name="jEffectiveDate2" value="" onBlur="DateFormatCheck(this)" style="width:100%"
><br/></td><td><img src="../../images/calendar1.gif" onClick="return showCalendar('jEffectiveDate2', '%m/%d/%Y %H:%M', '24');" width="13" height="13" />
</td>