Thursday, June 28, 2012

Save NSDate in CoreData

1. In your model file.
Create an entity MyDate with attribute datestring of datatype date(select it from dropdown).


2. Save your date


    //Create date from the string
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSDate *date=[dateFormatter dateFromString:dateString_];
    [dateFormatter release];
    dateFormatter=nil;
    
    
    
    //Save the auditDate Now    
    MyDate *obj = [NSEntityDescription insertNewObjectForEntityForName:@"MyDate" inManagedObjectContext:context];
    [obj setValue:date forKey:@"datestring"];
    
    NSError* error = nil;
    [context save:&error];
    if (error)
    {
        NSLog(@"%@", error);
    }




3. Retrieve your date

When you  retrieve  MyDate objects, accessing value of key datestring will return NSDate, you can convert that to NSString to display it on your screen.





Get number of days in a month

NSDate *today=[NSDate date];


NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSRange days = [gregorian rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:date];
int daysInGivenMonth= days.length;






Get first day of a month.

First we will build first date (01-xx-xxxx)of a given month.
Of course you can change 01 to anything like 12,26 or whatever date you want to find day of.


int month = 4;
int year=2012;



NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *date=[dateFormatter dateFromString:[NSString stringWithFormat:@"01-%i-%i",month,year]];
[dateFormatter release];
dateFormatter=nil;
   

Then we will find day for the above date in Gregorian Calendar. You can choose the method give in  previous article where you create NSDateFormatter with @"EEEE",But if you are calendar specific, then following is the way.

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =[gregorian components:NSWeekdayCalendarUnit fromDate:date];
int firstDay = [weekdayComponents weekday];//1 for Sun,2 for Mon etc
[gregorian release];

Get Day Name in NSDate

In this example we will find what day(Sun, Mon or Tue etc) is it for a given date.



    NSDate *today=[NSDate date];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEEE"];
    NSString *currentDay = [dateFormatter stringFromDate:today];
    NSLog(@"currentDay=%@", currentDay);
    //currentDay is 1 for Sun, 2 for Mon


    [dateFormatter release];
    

Get Month from NSDate

In this example we will take todays date and find the month in todays date.



    NSDate *today=[NSDate date];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"M"];
    NSString *currentMonth = [dateFormatter stringFromDate:today];
    //currentMonth will be 1 for Jan, 2 for Feb etc
    NSLog(@"currentMonth=%@",currentMonth);


    [dateFormatter release];
    

Boolean in NSDictionary

You can store a bool value in NSDictionary as following



NSDictionary *aProperties=[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:@"myboolkey"];

 To retrieve a bool value from NSDictionary:

BOOL boolValue;
if([aProperties valueForKey:@"myboolkey"]) 
  boolValue =[[aProperties valueForKey:@"myboolkey"] boolValue];