本文介绍了在javascript中将日期格式从dd-MMM-yyyy转换为dd-MM-yyyy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在jquery中比较开始日期和结束日期。 aspx页面中的代码如下所示:
I am trying to compare start date and end date in jquery. Code in aspx page looks like:
<asp:TextBox ID="tbStartDate" runat="server" CssClass="textbox" Width="80px" contentEditable="false" onchange="javascript:compareDates();"></asp:TextBox>
<asp:ImageButton runat="Server" ID="ibStartCalendar" ImageUrl="~/Images/calendar.jpg"
Height="18px" AlternateText="Click to show calendar" />
<act:CalendarExtender ID="ceStartDate" runat="server" Format="dd-MMM-yyyy" TargetControlID="tbStartDate" PopupButtonID="ibStartCalendar" CssClass="Calendar">
</act:CalendarExtender>
和javascript函数:
And javascript function:
function compareDates() {
var start = $("#<%=tbStartDate.ClientID %>").val();
var end = $("#<%=tbEndDate.ClientID %>").val();
if (!compareDate(start, end)) {
$("#<%=lblMsg.ClientID %>").html("Start Date can not be greater than End Date");
}
}
function compareDate(start, end) {
if (start.length > 0 && end.length > 0) {
var stDate = new Date(start);
var enDate = new Date(end);
var compDate = enDate - stDate;
if (compDate >= 0)
return true;
else {
return false;
}
} else { return true; }
}
要求是以dd-MMM-yyyy格式显示日期文本框我无法在calenderExtender中更改格式。
Requirement is to dispaly date in "dd-MMM-yyyy" format in text boxes to I can not change format in calenderExtender.
当我保持此格式时,星形日期和结束日期变为NaN。如果我在calenderExtender中更改格式,我会得到正确的结果。但正如我所说,我不能改变那里的格式。如何在javascript中更改格式?
When I keep this format star date and end date becomes "NaN". If I change format in calenderExtender i get proper results. However as I said I can not change format there. How can I change format in javascript?
推荐答案
以下是答案...
function GetDate(str) {
debugger;
var arr = str.split('-');
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun','Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
var i = 1;
for (i; i <= months.length; i++) {
if (months[i] == arr[1])
{
break;
}
}
var formatddate = i + '/' + arr[0] + '/' + arr[2];
return formatddate;
}
function StartDateTimeEndDate() {
var startDate = document.getElementById('<%=EFF_START_DATEDVTextBox.ClientID %>').value
var endDate = document.getElementById('<%=EFF_END_DATEDVTextBox.ClientID %>').value
var startdt = GetDate(startDate)
var enddt = GetDate(endDate)
if ((Date.parse(enddt) <= Date.parse(startdt))) {
alert("End date should be greater than Start date");
document.getElementById('<%=EFF_END_DATEDVTextBox.ClientID %>').value = "";
}
}
这篇关于在javascript中将日期格式从dd-MMM-yyyy转换为dd-MM-yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!