ravi
ravi

Reputation: 120

finding DateOFBirth From Age

Is there any formula to direct finding D.O.B from put Year,month and day's in TextField ?? InShort put 21 year 6 month 16 day in text filed that return the calculating D.O.B ?

Upvotes: 2

Views: 225

Answers (4)

Nimit Parekh
Nimit Parekh

Reputation: 16864

Try this it is more useful

Just enter the two dates it gives the difference.

 NSDateComponents *comp=[[NSCalendar currentCalendar] components:(NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date] toDate:self.objDate options:0] ;

 NSLog(@"%@",[NSString stringWithFormat:@"%d : %d : %d : %d : %d : %d",comp.year,comp.month,comp.day,comp.hour,comp.minute,comp.second]);

Following code may helping to you.

Happy coding

Upvotes: 1

Vignesh
Vignesh

Reputation: 10251

In iphone you do not have direct method how ever you can write on your own.

step 1: Make a NSDate object from year,month,day.

NSDateComponents *comp = [[NSDateComponents alloc] init];
[comp setYear:year];
[comp setMonth:month];
[comp setDay:day];
 NSDate *birthdate = [[NSCalendar currentCalendar] dateFromComponents:comp];
[comp release];

step 2: Find the interval from from today.

NSTimeInterval dateDiff = [birthDate timeIntervalSinceNow];
int age=trunc(dateDiff/(60*60*24))/365;

EDIT ...

    NSDateComponents *comp = [[NSDateComponents alloc] init];
    [comp setYear:-year];
    [comp setMonth:-month];
    [comp setDay:-day];
     NSDate *birthdate = [[NSCalendar currentCalendar]dateByAddingComponents:comp toDate:[NSDate date] options:0];;
    [comp release];

Upvotes: 5

Akhil
Akhil

Reputation: 14058

Try this.

Calendar c = Calendar.getInstance();
    System.out.println();
    c.add(Calendar.YEAR, -21);
    c.add(Calendar.MONTH, -6);
    c.add(Calendar.DAY_OF_MONTH, -16);
    System.out.println((c.get(Calendar.MONTH)+1)+"-"+ c.get(Calendar.DAY_OF_MONTH)+"-"+c.get(Calendar.YEAR));

Upvotes: 2

Ruben Romero
Ruben Romero

Reputation: 621

...In Java... I like to use Calendar for this operations. In your case I recommend you to use the add method

So you should create a new instance of Calendar with current date, set the day and the month, and then substract the age.

Upvotes: 2

Related Questions