如何将文本框日期与dd-MMM-yyyy格式的当前日期进行比较 [英] How to compare text box date with current date which is in dd-MMM-yyyy format
本文介绍了如何将文本框日期与dd-MMM-yyyy格式的当前日期进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将文本框日期与dd-MMM-yyyy格式的当前日期进行比较.
How to compare text box date with current date which is in dd-MMM-yyyy format.
推荐答案
嗨!
尝试以下代码:
Hi!
Try following code:
DateTime dt1 = new DateTime.Now;
String.Format("{0:dd-MMM-yyyy}", dt1);
DateTime dt2 = Convert.ToDateTime(txtDate.text);
String.Format("{0:dd-MMM-yyyy}", dt1);
int result = string.Compare(dt1, dt2,true);
string relationship;
if (result < 0)
relationship = "is earlier than";
else if (result == 0)
relationship = "is the same time as";
else
relationship = "is later than";
问候.
Regards.
^ ]
http://stackoverflow.com/questions/3509683/validate-two-dates-of-this-dd-mmm-yyyy-format-in-javascript[^]
function customParse(str) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
n = months.length, re = /(\d{2})-([a-z]{3})-(\d{4})/i, matches;
while (n--) { months[months[n]] = n; } // map month names to their index :)
matches = str.match(re); // extract date parts from string
return new Date(matches[3], months[matches[2]], matches[1]);
}
function dtdatecompare()
{
alert(document.getElementById('ctl00_SampleContent_Date1').value);
var dt =document.getElementById('ctl00_SampleContent_Date1').value;
var currentTime = new Date();
currentTime = currentTime.format('dd-MMM-yyyy');
/* var month = new Array();
month[0] = "Jan";
month[1] = "Feb";
month[2] = "Mar";
month[3] = "Apr";
month[4] = "May";
month[5] = "Jun";
month[6] = "Jul";
month[7] = "Aug";
month[8] = "Sep";
month[9] = "Oct";
month[10] = "Novr";
month[11] = "Dec";
*/
/// var cur = currentTime.split("-");
alert(currentTime.toString());
alert(customParse(dt));
alert(customParse(currentTime));
if (customParse(dt) > customParse(currentTime)) {
alert('invalid');
}
else {
alert('correct');
}
}
使用Convert.ToDateTime(string):=>
use Convert.ToDateTime(string) : =>
DateTime time = Convert.ToDateTime("10-10-2012");
if (DateTime.Now <= time)
{
Response.Write("its work");
}
这篇关于如何将文本框日期与dd-MMM-yyyy格式的当前日期进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文