表按日期排序(英国日期格式) [英] Table Sort With Dates (UK date format)
问题描述
我是一个愚蠢的ASP程序员,我不做Javascript(除了非常简单的任务),我有点困境。我从这里使用了一个
javascript表格排序脚本:
http://www.ipwebdesign.net/kaelisSpa...tableSort.html
除非它没有我的英国格式化日期
正确排序,我最终会得到这样的结果:
出生日期(年/月/日)
=======================
01/10/1990
01/05/1981
01/02/1956
01/08/1977
02/12/1944
作者显然预见到了这个问题并在剧本中留下了一个评论
,描述了如何解决这个问题(我已经复制了这篇文章底部的
注释) )。解决方案是:
如果字段有另一种格式的日期,则修改函数isDate。,
因此我有研究了IsDate函数,希望能够实现这个改变。但是这样做我可以诚实地说我没有线索
它是如何工作的。因此,我很感激任何人都可以提供的任何帮助
我将此脚本转换为使用英国日期格式(年/月/日)。
TIA,
科林
函数isDate(str)
{
/ *如果字段返回true是mm / dd / yyyy因为某些原因,
浏览器没有为这些字段提供预期的NaN * /
var re = / ^ [ 01]?[0-9] \ / [0-9]?[0-9] \ / [12] [0-9] [0-9] [0-9] $ /;
if(!str || str.length< 1)返回false;
如果(!re.test(strObject.value))返回false;
else返回true;
}
/ * parseFloat解析其参数,一个字符串,并返回一个浮动的
点数。如果遇到符号以外的字符(+
或 - ),
数字(0-9),小数点或指数,则返回
值
到目前为止并忽略该字符和所有后续
字符。因此,如果字段是mm / dd / yyyy格式的日期,则使用正则表达式来确定
,因为它不会正确地对字段进行排序
不返回NaN并将字段排序为
字符串,正如您可能期望的那样)。修改函数isDate如果
该字段有另一种格式的日期。 * /
/;
if(!str || str.length< 1)return false;
如果(!re.test(strObject.value))返回false;
else返回true;
}
/ * parseFloat解析其参数,一个字符串,并返回一个浮动的
点数。如果遇到符号以外的字符(+
或 - ),
数字(0-9),小数点或指数,则返回
值
到目前为止并忽略该字符和所有后续
字符。因此,如果字段是mm / dd / yyyy格式的日期,则使用正则表达式来确定
,因为它不会正确地对字段进行排序
不返回NaN并将字段排序为
字符串,正如您可能期望的那样)。修改函数isDate如果
该字段有另一种格式的日期。 * /
:我将此脚本转换为使用英国日期格式(年/月/日)。
:
:var re = / ^ [01]?[0-9] \ / [0-9]?[0-9] \ / [12] [0- 9] [0-9] [0-9]
/;
var re = / ^ [0123]?[0-9] ] \ / [01] [0-9] \ / [12] [0-9] [0-9] [0-9]
I''m a stupid ASP programmer and I dont do Javascript (except for very
simple tasks anyway), and I''m in a bit of a predicament. I''ve used a
javascript table sorting script from here:
http://www.ipwebdesign.net/kaelisSpa...tableSort.html
This works great except it doesn''t sort my UK formatted dates
properly, and I end up with something like this:
Birth Date (dd/mm/yyyy)
=======================
01/10/1990
01/05/1981
01/02/1956
01/08/1977
02/12/1944
The author obviously foresaw this problem and helpfully left a comment
in the script describing how to solve the problem (I have copied the
note at the bottom of this post). The solution is to:
"Modify function isDate if the field has a date in another format.",
I have therefore studied the IsDate function in hopes of making this
change. But having done this I can honestly say I dont have a clue
how it works. I would therefore appreciate any help anyone could give
me to convert this script to work with UK date format (dd/mm/yyyy).
TIA,
Colin
function isDate(str)
{
/* Returns true if the field is mm/dd/yyyy because for some reason,
the browser is not giving the expected NaN for these fields */
var re = /^[01]?[0-9]\/[0-9]?[0-9]\/[12][0-9][0-9][0-9]$/;
if (!str || str.length<1) return false;
if (!re.test(strObject.value)) return false;
else return true;
}
/* parseFloat parses its argument, a string, and returns a floating
point number. If it encounters a character other than a sign (+
or -),
numeral (0-9), a decimal point, or an exponent, it returns the
value
up to that point and ignores that character and all succeeding
characters. Therefore, a regular expression is used to determine
if the field is a date in mm/dd/yyyy format, since it will not sort
the field properly (it does not return NaN and sort the field as a
string, as you might expect it to do). Modify function isDate if
the field has a date in another format. */
/;
if (!str || str.length<1) return false;
if (!re.test(strObject.value)) return false;
else return true;
}
/* parseFloat parses its argument, a string, and returns a floating
point number. If it encounters a character other than a sign (+
or -),
numeral (0-9), a decimal point, or an exponent, it returns the
value
up to that point and ignores that character and all succeeding
characters. Therefore, a regular expression is used to determine
if the field is a date in mm/dd/yyyy format, since it will not sort
the field properly (it does not return NaN and sort the field as a
string, as you might expect it to do). Modify function isDate if
the field has a date in another format. */
: me to convert this script to work with UK date format (dd/mm/yyyy).
:
: var re = /^[01]?[0-9]\/[0-9]?[0-9]\/[12][0-9][0-9][0-9]
/;
var re = /^[0123]?[0-9]\/[01]?[0-9]\/[12][0-9][0-9][0-9]
这篇关于表按日期排序(英国日期格式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!