Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Regular Contributor
Posts: 65
Registered: ‎01-03-2013
My Device: 9810
My Carrier: Tmobile
Accepted Solution

how to compare two dates and find exact

how to compare two dates  and find exact age of person, person could be a kid age of 2 days or one month or whatever .

 

I will really appreciate if anyone o hlep 

 

Regards 

 

-----
Please press the like button to thank the user that helped you.
Accept as a solution if it solved the problem.
Cheers
Developer
Posts: 1,230
Registered: ‎03-20-2011
My Device: Playbook, Z10 LE, Dev Alpha B, 2x Dev Alpha C
My Carrier: 3, Orange, Vodafone

Re: how to compare two dates and find exact

Google provides an answer quickly enough (why do people never do this?)




Click the like button if you find my posts useful!
Regular Contributor
Posts: 65
Registered: ‎01-03-2013
My Device: 9810
My Carrier: Tmobile

Re: how to compare two dates and find exact

Dear Peardox, Thank you very much for your kind reply "appreciated".

 

the refernce provided in your reply only calculte year not months and days, for example if someone want to enter his child info "Which is just 2 months and 5 days" your refernced code output will give 0 which is not helpful at all.

 

Regarding to your question  "why do people never do this?" I am sure there are many answers to my quetions in google. but not same as what i am looking. or maybe i am not able to find it.  I do found few other posts in google but they dont work properly due to change in the number of days in a month "months can be 31,30,29 or 28 days".

 

-----
Please press the like button to thank the user that helped you.
Accept as a solution if it solved the problem.
Cheers
Developer
Posts: 1,230
Registered: ‎03-20-2011
My Device: Playbook, Z10 LE, Dev Alpha B, 2x Dev Alpha C
My Carrier: 3, Orange, Vodafone

Re: how to compare two dates and find exact

Well the rest of it isn't exactly hard to write is it?




Click the like button if you find my posts useful!
Regular Contributor
Posts: 65
Registered: ‎01-03-2013
My Device: 9810
My Carrier: Tmobile

Re: how to compare two dates and find exact

[ Edited ]

After spending 2 hours I come out with a solution by myself, how ever the function can be customize to check if the user is entering right date.

 

function findAge(subjectName,fromdate, todate) {
	console.log("findAge(fromdate, todate) is called now "+subjectName+"-->"+fromdate+"-->"+todate);
    if(todate) todate= new Date(todate);
    else todate= new Date();

    var age= [], fromdate= new Date(fromdate),
    y= [todate.getFullYear(), fromdate.getFullYear()],
    ydiff= y[0]-y[1],
    m= [todate.getMonth(), fromdate.getMonth()],
    mdiff= m[0]-m[1],
    d= [todate.getDate(), fromdate.getDate()],
    ddiff= d[0]-d[1];

    if(mdiff < 0 || (mdiff=== 0 && ddiff<0))--ydiff;
    if(mdiff<0) mdiff+= 11;
    if(ddiff<0){
        fromdate.setMonth(m[1]+1, 0);
        ddiff= fromdate.getDate()-d[1]+d[0];
        --mdiff;
    }
    if(ydiff> 0) age.push(ydiff+ ' year'+(ydiff> 1? 's ':' '));
    if(mdiff> 0) age.push(mdiff+ ' month'+(mdiff> 1? 's':''));
    if(ddiff> 0) age.push(ddiff+ ' day'+(ddiff> 1? 's':''));
    if(age.length>1) age.splice(age.length-1,0,' and ');
    console.log("===============================");
    console.log("Subject age is = "+age.join(''));
    console.log(" age Day = "+ddiff);
    console.log(" age Month = "+mdiff);
    console.log(" age Year = "+ydiff);
    console.log("===============================");
    var subjectAGE =  age.join('');
    
}

 

peardox Thank you for reply Smiley Indifferent

-----
Please press the like button to thank the user that helped you.
Accept as a solution if it solved the problem.
Cheers
Developer
Posts: 1,230
Registered: ‎03-20-2011
My Device: Playbook, Z10 LE, Dev Alpha B, 2x Dev Alpha C
My Carrier: 3, Orange, Vodafone

Re: how to compare two dates and find exact

The worst thing with dates is Leap Years as there are some funny rules for them (they're not every 4 years...)




Click the like button if you find my posts useful!