By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,856 Members | 1,986 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,856 IT Pros & Developers. It's quick & easy.

To compare two Dates

P: 4
Hi,

I used Date object of Javascrips to compare two dates like one is the from date and the other is to date.
var obj1 = new Date(from date); [ the dates are given using split functions]
var obj2 = new Date(to date);

then i used getTime to compare these two dates.
Its working fine for all the cases except
if the from date is 31st of a month and to date is the 1st of the following month.
(this occurs for case like 31st Mar and 1st Apr
31st May and 1st June, Aug 31st and Sep 1st, oct 31s and nov 1st)
So please anybody give me a solution for this.
I will be pleased if someone gives the solution for this.
Thanks
Thiru...
Apr 2 '07 #1
Share this Question
Share on Google+
6 Replies


acoder
Expert Mod 15k+
P: 16,027
Welcome to TSDN!

Just use > or < to compare, e.g.
Expand|Select|Wrap|Line Numbers
  1. if (obj1 > obj2) {
see link.
Apr 3 '07 #2

P: 4
Welcome to TSDN!

Just use > or < to compare, e.g.
Expand|Select|Wrap|Line Numbers
  1. if (obj1 > obj2) {
see link.
Hi,

I used that only.
ie
if (obj1.getTime() < obj2.getTime())
do something
else
do something.

Its working fine with all the cases.
But its not working for some cases like, If i give
From date: 31-03-2007 and
To date: 1-04-2007

Thanks and Regards
Thiru...
Apr 3 '07 #3

100+
P: 428
The kind of string you can parse into a date object depends
on your language and operating system, but any implementation
can build a date from new Date(fullyear,month,day).
If you have a string you know is day-month-year,
the safest way to get the date is to split the
string and reassemble the pieces:
(don't forget that months are 0 based- January is 0 in javascript).

Expand|Select|Wrap|Line Numbers
  1. function reDate(dmy){
  2.     var d=dmy.split(/\D+/);
  3.     return new Date(d[2],d[1]-1,d[0]);
  4. }
var d1='31-03-2007';
var d2='1-04-2007';
reDate(d2)> reDate(d1) returns true;
reDate(d2)- reDate(d1) returns 86400000
Apr 3 '07 #4

P: 4
The kind of string you can parse into a date object depends
on your language and operating system, but any implementation
can build a date from new Date(fullyear,month,day).
If you have a string you know is day-month-year,
the safest way to get the date is to split the
string and reassemble the pieces:
(don't forget that months are 0 based- January is 0 in javascript).

Expand|Select|Wrap|Line Numbers
  1. function reDate(dmy){
  2.     var d=dmy.split(/\D+/);
  3.     return new Date(d[2],d[1]-1,d[0]);
  4. }
var d1='31-03-2007';
var d2='1-04-2007';
reDate(d2)> reDate(d1) returns true;
reDate(d2)- reDate(d1) returns 86400000
Thank you.
Its working fine.
The only mistake which i did is not considering that months are 0 based
ie jan - 0. I didn't subtracted one from the months.
Thanks a lot.
Regards
Thiru....
Apr 4 '07 #5

P: 4
one more doubt
i wanted to know why its not working with few cases
like 31-03-2007 and 01-4-2007, or
with those two months having 30 and 31 number of days.
can u explain the reason for that.
Regards
thiru...
Apr 4 '07 #6

P: 1
hey thiru take it easy it is not so tough job,
as you are created the two dates then,
use the date() function to create the date in specific formate and then just add date by using + operateor and then assign date to it
Mar 10 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.