/    Sign up×
Community /Pin to ProfileBookmark

Convert and compare two Dates

I accept two dates from an input form (fromsvcdt and tosvcdt) in mm/dd/yyyy and I load them into the following variables. What is the best method to compare these dates because comparing the dates as they are doesn’t work?

var strToUsrDt = window.document.frmBexSrch.txtToDateSvc.value;
var strFromUsrDt = window.document.frmBexSrch.txtFromDateSvc.value;

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@popeyeauthorOct 10.2005 — This is what I am currently doing and it seems to work. I am not sure if this is the best way and I really don't understand the eval() statement?? Any input would be great.

var strBirthDt = window.document.frmBexSrch.txtSubBirth.value;

ChkUsrDt = new Date(strBirthDt);

var bmonth = ChkUsrDt.getMonth();

bmonth = bmonth + 1; //add 1 because it's relative to 0

var bday = ChkUsrDt.getDate();

var byear = ChkUsrDt.getFullYear();

var BirthDt = (eval(bday) + (eval(bmonth) * 30)) + (eval(byear) * 365);

if (BirthDt > UsrCurrDte)

{

alert("Subscriber BirthDate must be equal or Less than Todays Date");

return false;

}
Copy linkTweet thisAlerts:
@KorOct 10.2005 — 
because comparing the dates as they are doesn't work
[/quote]


Just for you knowledge, comparing those values [b]will[/b] work, if understanding that all the HTML attributes have [i]strings[/i] as values, not numbers, so that they must be transformed in numbers before any math add/substraction/comparision.

In case of time date, of course, you were right, it is easier to transform those values in valid Data variables, then compare them, using the internal clock of the computer rather that an intricate triple comparision operation.

So that your ideea is right, but you did some incorrect (or inapropiate) things:

The new Date() object is to be used with the following parameters:

new Date()

new Date(milliseconds)

new Date(dateString)

new Date(year, month, day, hours, minutes, seconds, milliseconds)

Most parateners are optional. If no specified, they return 0;

Now, for your problem you must use your values like that:

var myDate = new Date(yyyy,mm-1,dd) if the values are numbers or

var myDate = new Date('yyyy/mm/dd') if the values are string type

So that all you have to do is to build your string parameter in a correct way ('yyyy/mm/dd') by [COLOR=Red]rearranging[/COLOR] the string, then simply compare the Date objects.

[code=php]
...
var inputDate = document.frmBexSrch.txtSubBirth.value.split('/');
var birthDate = new Date(inputDate[2]+'/'+inputDate[0]+'/'+inputDate[1]);
var today = new Date();
if(birthDate<today){return true}
else
{
alert("Subscriber BirthDate must Equal or Less than Todays Date");
return false;
}
...
[/code]
Copy linkTweet thisAlerts:
@popeyeauthorOct 11.2005 — Is this what I would code in JavaScript?

var inputDate = document.frmBexSrch.txtSubBirth.value.split('/');

var birthDate = new Date(inputDate[2]+'/'+inputDate[0]+'/'+inputDate[1]);

var today = new Date();

Let me make sure I understand;

statement 1; creates inputDate without the Slashes by using the (split)'?

statement 2; creates birthDate in yyyy/mm/dd [2] = yr,[0] = month, [1] day?

statement 3; today creates current date in yyyy/mm/dd?

and then just do my compares?

Thank you
Copy linkTweet thisAlerts:
@KorOct 11.2005 — You said that yoiur input values are mm/dd/yyyy

So that:

1: splits the string in an array whom values are now ['mm','dd','yyyy']

2. recombines the string which becomes now 'yyyy/mm/dd' and than make it a Date object

2. creates the current day
×

Success!

Help @popeye spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...