473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

To compare two Dates

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
6 5594
acoder
16,027 Expert Mod 8TB
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
cd123
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
mrhoo
428 256MB
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
cd123
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
cd123
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
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

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

Similar topics

4
by: Gleep | last post by:
Hey Guys, I've got a table called Outcomes. With 3 columns and 15 rows 1st col 2nd col 3rdcol outcome date price There are 15 rows...
3
by: jrc4728 | last post by:
I have a MySQL table with the date stored in three fields as string values like this. (sorry, its imported data) str_yy str_dd str_mm ------------------------ 05 01 04 05 ...
4
by: alexis | last post by:
Hi, In a form I have the curent date <input name="datetoday" type="hidden" value="<? echo date("d/m/Y"); ?>"> and <input type=text name="datebox" size=15> The date format is d/m/Y...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
9
by: Rich | last post by:
Thanks for the Help in my previous post. I've been working on this and it's almost what I want. I want to obtain the user's current age by comparing their date of birth (user inputs) to the...
9
by: Rimuen | last post by:
Have two text form with dates i need to compare before submitting, the second should always be a date later the first one. Anyone have a script for this ?? Thanks.......
1
by: godsella | last post by:
First i have two stored procedures, i have passed the values of each one into two different arraylists of dates. how can i compare the two arraylists of dates? Thanks in advance
5
by: Tom | last post by:
It appears that you can't compare two dates in DotNet. You must use ToString and compare the strings. Is that the only reliable way? Try this: Dim dteOne As Date =...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.