排序日期 [英] sorting dates

查看:74
本文介绍了排序日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的日期格式为01-Jan-05,我的排序程序没有正确排序




函数compareDate(a,b)

{

var date_a = new Date(a);

var date_b = new Date(b);

if(date_a< date_b)

{return -1; }

其他

{

if(date_a> date_b)

{return 1; }

else

{return 0; }

}

}


我猜它希望以mm / dd / yyyy格式表示日期。
<我需要更改:


var date_a = new Date(a);

var date_b = new Date(b) ;


所以它能识别出正确的格式吗?


Mike

If I had a date in the format "01-Jan-05" it does not sort properly
with my sort routine:

function compareDate(a,b)
{
var date_a = new Date(a);
var date_b = new Date(b);
if (date_a < date_b)
{ return -1; }
else
{
if (date_a > date_b)
{ return 1; }
else
{ return 0; }
}
}

I guess it expects the date in mm/dd/yyyy format.

Do I have to change:

var date_a = new Date(a);
var date_b = new Date(b);

so it recognizes a correct format?

Mike

推荐答案

mike写道:
如果我的日期格式为01-Jan-05它没有按照我的排序程序正确排序:


Date()的参数是:


日期([年份] [,month [,date [,hours [,minutes [,seconds [,ms]]]]]]]]


所有参数都是可选的。
function compareDate(a,b)
{var date_a = new Date(a);


假设输入已发布并注意到月份从0开始编号为
到11:


var months = {''jan'':0,''feb'':1,''mar'':2,''apr'':3,''may'':4,''jun'':5,

''jul'':6,''aug'':7,''sep'':8,''oct'':9,''nov'':10,'' dec'':11};

var aBits = a.split('' - '');

var date_a = new Date(a [2],months [ a [1] .toLowerCase()],a [0]);


和b相同。请注意,您应首先验证所有输入,并且

''date_a''是您打算使用它的任何目的的有效日期。


例如如果输入''00 -Jul-2005'',结果日期将是2005年6月30日。

''32 -Jul-2005''给出2005年8月1日
var date_b = new Date(b);
if(date_a< date_b)
{return -1; }

{
if(date_a> date_b)
{return 1; }
其他
{return 0; }
}

我想它希望日期格式为mm / dd / yyyy。
If I had a date in the format "01-Jan-05" it does not sort properly
with my sort routine:
The arguments for Date() are:

Date( [year[, month[, date[, hours[, minutes[, seconds[, ms]]]]]]])

All arguments are optional.

function compareDate(a,b)
{
var date_a = new Date(a);
Assuming input is as posted and noting that months are numbered from 0
to 11:

var months = {''jan'':0, ''feb'':1, ''mar'':2, ''apr'':3, ''may'':4, ''jun'':5,
''jul'':6, ''aug'':7, ''sep'':8, ''oct'':9, ''nov'':10, ''dec'':11 };
var aBits = a.split(''-'');
var date_a = new Date( a[2], months[a[1].toLowerCase()], a[0] );

and the same for b. Note that you should validate all input first and
that ''date_a'' is a valid date for whatever purpose you intend to use it for.

e.g. if ''00-Jul-2005'' is input, the resulting date will be 30-Jun-2005.
''32-Jul-2005'' gives 01-Aug-2005
var date_b = new Date(b);
if (date_a < date_b)
{ return -1; }
else
{
if (date_a > date_b)
{ return 1; }
else
{ return 0; }
}
}

I guess it expects the date in mm/dd/yyyy format.




否,''它'不是。这里的日期还有很多东西:


< URL:http://www.merlyn.demon.co.uk/js-dates.htm>

[...]


-

Rob



No, ''it'' doesn''t. There is a lot more stuff on dates here:

<URL:http://www.merlyn.demon.co.uk/js-dates.htm>
[...]

--
Rob


Rob谢谢我可以理解那里的内容。


工作正常。

Rob thanks i can understand what is goin on there.

works fine.


RobG写道:
mike写道:
mike wrote:
如果我的日期格式为01-Jan-05它没有按照我的排序程序正确排序:


作为提示,只需重新格式化为yyyy-mm-dd将正确排序(并且是
$ b以国际公认的ISO可接受格式提供的$ b)。

根本不需要使用日期对象(尽管您可能希望使用

来验证您的日期,然后再尝试对它们进行排序)。


下面是一个简单的例子,没有尝试输入或日期验证

但是在实践中需要。


我有保持月份为零索引 - 您可能希望更改它。

< script type =" text / javascript">


var months = {''jan'':0,''feb'':1,''mar'':2,''apr'':3,

''may'':4, ''jun'':5,''jul'':6,''aug'':7,

''sep'':8,''oct'':9,'' nov'':10,''dec'':11};


函数sortDates(){

var D = [];

var i = arguments.length;

while(i - ){

D [i] = formatAsYMD(arguments [i]);

}

返回D.sort();

}


函数格式AsYMD(st r){

var x = str.split('' - '');

返回x [2] +'' - ''

+ addZ(months [x [1] .toLowerCase()])+'' - ''

+ addZ(x [0]);

}


函数addZ(x){

return(x< 10)? ''0''+ x:x;

}


< / script>


< form action ="">

< input name =" dStrA" value =" 28-Jul-2005">

< input name =" dStrB" value =" 28-Aug-2005">

< input name =" dStrC" value =" 2-Feb-2003">

< input type =" button" value =" show date" onclick ="

alert(sortDates(

this.form.dStrA.value,

this.form.dStrB.value,

this.form.dStrC.value



);

">


< / form>
If I had a date in the format "01-Jan-05" it does not sort properly
with my sort routine:
As a hint, simply reformatting as yyyy-mm-dd will sort correctly (and be
in an ISO acceptable format that is recognised internationally). There
is no need for using date objects at all (though you may wish to use
them to validate your dates before attempting to sort them).

Below is a simple example, no validation of input or dates is attempted
but would be required in practice.

I have kept the months as zero-indexed - you may wish to change that.
<script type="text/javascript">

var months = { ''jan'':0, ''feb'':1, ''mar'':2, ''apr'':3,
''may'':4, ''jun'':5, ''jul'':6, ''aug'':7,
''sep'':8, ''oct'':9, ''nov'':10, ''dec'':11 };

function sortDates(){
var D = [];
var i = arguments.length;
while ( i-- ) {
D[i] = formatAsYMD( arguments[i] );
}
return D.sort();
}

function formatAsYMD( str ) {
var x = str.split(''-'');
return x[2] + ''-''
+ addZ( months[x[1].toLowerCase()] ) + ''-''
+ addZ( x[0] );
}

function addZ( x ) {
return ( x < 10 )? ''0'' + x : x;
}

</script>

<form action="">
<input name="dStrA" value="28-Jul-2005">
<input name="dStrB" value="28-Aug-2005">
<input name="dStrC" value="2-Feb-2003">
<input type="button" value="show date" onclick="
alert( sortDates(
this.form.dStrA.value,
this.form.dStrB.value,
this.form.dStrC.value
)
);
">

</form>


Date()的参数是:

日期([ year [,month [,date [,hours [,minutes [,seconds [,ms]]]]]]])

所有参数都是可选的。


The arguments for Date() are:

Date( [year[, month[, date[, hours[, minutes[, seconds[, ms]]]]]]])

All arguments are optional.

函数compareDate(a,b)
{var /> var date_a = new Date(a);

function compareDate(a,b)
{
var date_a = new Date(a);



假设输入已发布并注明那几个月的数字是从0
到11:

var months = {''jan'':0,''feb'':1,''mar'':2,' 'apr'':3,''may'':4,''jun'':5,
''jul'':6,''aug'':7,''sep'':8 ,''oct'':9,''nov'':10,''dec'':11};
var aBits = a.split('' - '');
var date_a =新日期( a [2],月[a [1] .toLowerCase()],a [0]);

和b相同。请注意,您应首先验证所有输入,并且date_a是您打算使用它的任何目的的有效日期


如果输入''00 -Jul-2005'',结果日期将是2005年6月30日。
''32 -Jul-2005''给出2005年8月1日


Assuming input is as posted and noting that months are numbered from 0
to 11:

var months = {''jan'':0, ''feb'':1, ''mar'':2, ''apr'':3, ''may'':4, ''jun'':5,
''jul'':6, ''aug'':7, ''sep'':8, ''oct'':9, ''nov'':10, ''dec'':11 };
var aBits = a.split(''-'');
var date_a = new Date( a[2], months[a[1].toLowerCase()], a[0] );

and the same for b. Note that you should validate all input first and
that ''date_a'' is a valid date for whatever purpose you intend to use it
for.

e.g. if ''00-Jul-2005'' is input, the resulting date will be 30-Jun-2005.
''32-Jul-2005'' gives 01-Aug-2005

var date_b = new Date(b);
if(date_a< date_b)
{return -1; }

{
if(date_a> date_b)
{return 1; }
其他
{return 0; }
}
}
var date_b = new Date(b);
if (date_a < date_b)
{ return -1; }
else
{
if (date_a > date_b)
{ return 1; }
else
{ return 0; }
}
}




这可能是:


return( a< b);

我想它的预期日期为mm / dd / yyyy格式。



And that could be:

return ( a < b );

I guess it expects the date in mm/dd/yyyy format.



不,''它''没有''吨。这里的日期还有很多东西:

< URL:http://www.merlyn.demon.co.uk/js-dates.htm>

[...]


No, ''it'' doesn''t. There is a lot more stuff on dates here:

<URL:http://www.merlyn.demon.co.uk/js-dates.htm>
[...]



-

Rob


--
Rob


这篇关于排序日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆