Jump to content
bodalal

What's wrong with this Code? date calculate

3 posts in this topic

Recommended Posts

hi all

 

My app contain Calendar date picker and label

 

when i chose date from Calendar picker as "Birth date"

 

then to calculate the age , it should calculate date between today (now) and the date in calendar picker

 

i did everything right in interface builder, then i wrote this code :

 

#import "AgeCalc.h"

@implementation AgeCalc

- (IBAction)calcut:(id)sender {

NSDate *dBirth = [NSDate date];
NSDate *today = [NSDate date];

NSCalendar *gregorian = [[NSCalendar alloc]
						 initWithCalendarIdentifier:NSGregorianCalendar];

unsigned int unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;

NSDateComponents *components = [gregorian components:unitFlags fromDate:dBirth toDate:today options:0];
int years = [components year];
int months = [components month];
int days = [components day];

[txtResult setIntValue:years & @" Years " & months & @" Months" & days & @"Days"];

}
@end

 

 

myapp.jpg

 

 

BUT it does not work, nothing show up in the label

 

can any one help me plz.

 

thanx

Share this post


Link to post
Share on other sites
Advertisement
hi all

 

My app contain Calendar date picker and label

 

when i chose date from Calendar picker as "Birth date"

 

then to calculate the age , it should calculate date between today (now) and the date in calendar picker

 

i did everything right in interface builder, then i wrote this code :

 

#import "AgeCalc.h"

@implementation AgeCalc

- (IBAction)calcut:(id)sender {

NSDate *dBirth = [NSDate date];
NSDate *today = [NSDate date];

NSCalendar *gregorian = [[NSCalendar alloc]
						 initWithCalendarIdentifier:NSGregorianCalendar];

unsigned int unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;

NSDateComponents *components = [gregorian components:unitFlags fromDate:dBirth toDate:today options:0];
int years = [components year];
int months = [components month];
int days = [components day];

[txtResult setIntValue:years & @" Years " & months & @" Months" & days & @"Days"];

}
@end

 

 

myapp.jpg

 

 

BUT it does not work, nothing show up in the label

 

can any one help me plz.

 

thanx

 

 

Because it should be something like this (i mean should cuz i can't see the whole project):

 

[txtResult setStringValue: [NSString stringWithFormat: @"Years: %i Months: %i Days: %i", years, months , days]];

 

Hope it helped....

Share this post


Link to post
Share on other sites
Because it should be something like this (i mean should cuz i can't see the whole project):

 

[txtResult setStringValue: [NSString stringWithFormat: @"Years: %i Months: %i Days: %i", years, months , days]];

 

Hope it helped....

 

 

it works .. thaaaaaaaaaaaaaaaaaaanx alloooooooooooooooot ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×