[英]How compare two dates?
I have a two date like from and to date. 我有两个日期,例如from和date。 date format like 28-Sep-2013 and 01-Sep-2013 . 日期格式,例如2013年9月28日和2013年 9月1日 。 I want to compare date and show error message. 我想比较日期并显示错误消息。 I have use this code, 我已经使用了这段代码,
var from = $('#<%= txtFDateEdu.ClientID %>').val();
var to = $('#<%= txtTDateEdu.ClientID %>').val();
var dateTypeVar = $('#<%= txtFDateEdu.ClientID %>').datepicker('getDate');
var dateTypeVarto = $('#<%= txtFDateEdu.ClientID %>').datepicker('getDate');
$.datepicker.formatDate('dd-mm-yy', dateTypeVar);
var datefrom = $.datepicker.formatDate('dd/mm/yyyy', dateTypeVar);
var dateto = $.datepicker.formatDate('dd/mm/yyyy', dateTypeVarto);
alert(datefrom);
if (datefrom > dateto) {
alert("Success");
}
else {
}
This code is not working properly. 此代码无法正常工作。 Some issue occur in date format. 日期格式出现一些问题。 Please any one help me to solve date format issue and compare date properly. 请任何人帮助我解决日期格式问题并正确比较日期。 i also use this code. 我也使用此代码。
var from = $('#<%= txtFDateEdu.ClientID %>').val();
var to = $('#<%= txtTDateEdu.ClientID %>').val(;)
alert(from);
alert(to);
var datefrom = new Date(from);
var dateto = new Date(to);
alert(datefrom);
alert(dateto);
if (datefrom > dateto) {
alwer("Success");
}
else {
}
but this also not work properly. 但这也无法正常工作。
You are converting a given date to a string, just skip this step: 您要将给定的日期转换为字符串,只需跳过此步骤:
var datefrom = $('#<%= txtFDateEdu.ClientID %>').datepicker('getDate');
var dateto = $('#<%= txtFDateEdu.ClientID %>').datepicker('getDate');
if (datefrom > dateto) {
alert("Success");
}
else {
}
Just compare the two Date
objects before you've converted them to strings: 只需比较两个Date
对象, 然后再将它们转换为字符串即可:
var dateTypeVar = $('#<%= txtFDateEdu.ClientID %>').datepicker('getDate');
var dateTypeVarto = $('#<%= txtFDateEdu.ClientID %>').datepicker('getDate');
if (dateTypeVar > dateTypeVarto) {
...
}
The comparison operator will automatically take the value of the two date objects via .valueOf()
, that being the milliseconds elapsed since 1970/01/01 00:00:00. 比较运算符将通过.valueOf()
自动获取两个日期对象的值,这是自1970/01/01 00:00:00以来经过的毫秒数。
I also use date picker, normally i use following script. 我也使用日期选择器,通常我使用以下脚本。
function compare_dates(valid_to, valid_from){
var splited_to = valid_to.split("-")
var to_date_monthfield=splited_to[1];
var to_date_dayfield=splited_to[0];
var to_date_yearfield=splited_to[2];
var to_date = new Date(to_date_yearfield, to_date_monthfield-1, to_date_dayfield);
var splited_from = valid_from.split("-");
var from_date_monthfield=splited_from[1];
var from_date_dayfield=splited_from[0];
var from_date_yearfield=splited_from[2];
var from_date = new Date(from_date_yearfield, from_date_monthfield-1, from_date_dayfield);
var cur_date = new Date();
if(!(from_date > cur_date.setDate(cur_date.getDate() - 1))){
return false;
}
if(!(to_date > from_date)){
return false;
}
return true;
}
This expects your date date to be in dd//mm/yyyy format 这希望您的日期日期采用dd // mm / yyyy格式
here i found something use full. 在这里,我发现一些东西用光了。 You can use it with my code. 您可以将其与我的代码一起使用。
new Date(Date.parse(month_string +" 1, 2012")).getMonth()+1
you can replace monthfield with above code. 您可以使用上述代码替换monthfield 。 or you also can use a other way. 或者您也可以使用其他方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.