TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Comparing Two Dates In Jquery
Shantanu Puranik
Aug 12
2016
Code
699
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
function
CompareDate(field1, field2, currentField) {
var
objDate1;
var
objDate2;
var
dateParts = [];
var
months = { Jan: 1, Fab: 2, Mar: 3, Apr: 4, May: 5, Jun: 6, Jul: 7, Aug: 8, Sep: 9, Oct: 10, Nov: 11, Dec: 12 };
dateParts = field1.val().split(
"-"
);
dateParts[1] = months[dateParts[1]];
objDate1 =
new
Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
dateParts = field2.val().split(
"-"
);
dateParts[1] = months[dateParts[1]];
objDate2 =
new
Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
if
(objDate1 > objDate2) {
currentField.val(
''
);
return
false
;
}
else
{
return
true
;
}
}
Call the CompareDate
function
as below:
function
chkFromDate(obj) {
if
($(document.getElementById(
"txtToDate"
)).val() !=
null
) {
if
(!CompareDate($(
"#txtFrmDate"
), $(
"#txtToDate"
), $(
this
))) {
alert(
"From Date should be less than To Date."
);
}
}
}
Comparing Two Dates
Jquery