javascript 中日期类型处理浅谈
一次偶然的机会,在网上找到了一位仁兄贴出来的,判断function.
以下是我修改过的function.
function isDate(strDate)
{
var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
var r = s.match(reg);
if(r==null)return false;
//var d= new Date(r[1], --r[3],r[4],r[5],r[6]); //格式:2007-3-2 18:23
var d= new Date(r[1], --r[3],r[4],r[5],r[6],r[7]);
if(d.getFullYear()!=Number(r[1]))return false;//判断年份
if(d.getMonth()!=Number(r[3]))return false;//判断月份
if(d.getDate()!=Number(r[4]))return false;//判断日期
if(d.getHours()!=Number(r[5]))return false;//判断小时
if(d.getMinutes()!=Number(r[6]))return false;//判断分
if(d.getSeconds()!=Number(r[7]))return false;//判断秒
return true;
}
到目前,我还不能完整的说出r[1]到r[6]的由来,为了增加判断时间中秒的部分,我增加了reg是最后的"}):(\d{1,2})"部分,d中的r[7],以及"if(d.getSeconds()!=Number(r[7]))return false;//判断秒"
各方法解释如参考资料.
参考资料:
Method |
Description |
FF |
N |
IE |
Returns today's date and time |
1 |
2 |
3 |
|
Returns the day of the month from a Date object (from 1-31) |
1 |
2 |
3 |
|
Returns the day of the week from a Date object (from 0-6) |
1 |
2 |
3 |
|
Returns the month from a Date object (from 0-11) |
1 |
2 |
3 |
|
Returns the year, as a four-digit number, from a Date object |
1 |
4 |
4 |
|
Returns the year, as a two-digit or a four-digit number, from a Date object. Use getFullYear() instead !! |
1 |
2 |
3 |
|
Returns the hour of a Date object (from 0-23) |
1 |
2 |
3 |
|
Returns the minutes of a Date object (from 0-59) |
1 |
2 |
3 |
|
Returns the seconds of a Date object (from 0-59) |
1 |
2 |
3 |
|
Returns the milliseconds of a Date object (from 0-999) |
1 |
4 |
4 |
|
Returns the number of milliseconds since midnight Jan 1, 1970 |
1 |
2 |
3 |
|
Returns the difference in minutes between local time and Greenwich Mean Time (GMT) |
1 |
2 |
3 |
|
Returns the day of the month from a Date object according to universal time (from 1-31) |
1 |
4 |
4 |
|
Returns the day of the week from a Date object according to universal time (from 0-6) |
1 |
4 |
4 |
|
Returns the month from a Date object according to universal time (from 0-11) |
1 |
4 |
4 |
|
Returns the four-digit year from a Date object according to universal time |
1 |
4 |
4 |
|
Returns the hour of a Date object according to universal time (from 0-23) |
1 |
4 |
4 |
|
Returns the minutes of a Date object according to universal time (from 0-59) |
1 |
4 |
4 |
|
Returns the seconds of a Date object according to universal time (from 0-59) |
1 |
4 |
4 |
|
Returns the milliseconds of a Date object according to universal time (from 0-999) |
1 |
4 |
4 |
|
Takes a date string and returns the number of milliseconds since midnight of January 1, 1970 |
1 |
2 |
3 |
|
Sets the day of the month in a Date object (from 1-31) |
1 |
2 |
3 |
|
Sets the month in a Date object (from 0-11) |
1 |
2 |
3 |
|
Sets the year in a Date object (four digits) |
1 |
4 |
4 |
|
Sets the year in the Date object (two or four digits). Use setFullYear() instead !! |
1 |
2 |
3 |
|
Sets the hour in a Date object (from 0-23) |
1 |
2 |
3 |
|
Set the minutes in a Date object (from 0-59) |
1 |
2 |
3 |
|
Sets the seconds in a Date object (from 0-59) |
1 |
2 |
3 |
|
Sets the milliseconds in a Date object (from 0-999) |
1 |
4 |
4 |
|
Calculates a date and time by adding or subtracting a specified number of milliseconds to/from midnight January 1, 1970 |
1 |
2 |
3 |
|
Sets the day of the month in a Date object according to universal time (from 1-31) |
1 |
4 |
4 |
|
Sets the month in a Date object according to universal time (from 0-11) |
1 |
4 |
4 |
|
Sets the year in a Date object according to universal time (four digits) |
1 |
4 |
4 |
|
Sets the hour in a Date object according to universal time (from 0-23) |
1 |
4 |
4 |
|
Set the minutes in a Date object according to universal time (from 0-59) |
1 |
4 |
4 |
|
Set the seconds in a Date object according to universal time (from 0-59) |
1 |
4 |
4 |
|
Sets the milliseconds in a Date object according to universal time (from 0-999) |
1 |
4 |
4 |
|
Represents the source code of an object |
1 |
4 |
- |
|
Converts a Date object to a string |
1 |
2 |
4 |
|
Converts a Date object, according to Greenwich time, to a string. Use toUTCString() instead !! |
1 |
2 |
3 |
|
Converts a Date object, according to universal time, to a string |
1 |
4 |
4 |
|
Converts a Date object, according to local time, to a string |
1 |
2 |
3 |
|
Takes a date and returns the number of milliseconds since midnight of January 1, 1970 according to universal time |
1 |
2 |
3 |
|
Returns the primitive value of a Date object |
1 |
2 |
4 |
参考资料引用Url:<br>
http://www.w3schools.com/jsref/jsref_obj_date.asp