hope every one is doing well :)
I have struck with retrieving the month from the date that is saved in sqlite database.First of all,I would like to thank some of the
First of all we need to form a date manually.To do that we need to create NSDateComponents consisting of the required components in the following way:
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
-> Get the current year or present year using the components i.e:
NSInteger year = [components year];
-> Form the 3 strings year,month,day manually to get From date:
NSString *fromString = [NSString stringWithFormat:@"%d",year];
NSString *string2 = [NSString stringWithFormat:@"-%@",appDelegate.monthValue];
NSString *string3 = @"-01";
appDelegate.monthValue is nothing but the string assigned in appDelegate and assigned with the value selected in pickerView which contains 01,02,03.....12 month values
As we know 01 is nothing but the day(min range) in any month,we can simply hard-code it:)
Now we are ready to form the From Date,here we go:
string2 = [string2 stringByAppendingString:string3];
fromString = [fromString stringByAppendingString:string2];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init]autorelease];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *dateString=[NSString stringWithString:fromString];
NSDate *fromDate = [dateFormatter dateFromString:dateString];
NSString* finalFromDateString = [dateFormatter stringFromDate:fromDate];
Please observe the additional change that I have made to form the date,used 2 strings because we will have a problem when assigning string to NSDate,(improper date retrieval i.e. one day less than the actual date). For more details please follow this link . Now we are finished with from date.
-> Get the to date using month as reference
To get to date or max range in a month,we need to use NSRange method which will calculate the range for a month or number of days for a month in that particular year,here it is:
NSString *toString = [NSString stringWithFormat:@"%d",year];
NSString *monthValue = [NSString stringWithFormat:@"-%@",appDelegate.monthValue];
NSRange monthRange = [gregorian rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:fromDate];
NSString *rangeString = [NSString stringWithFormat:@"-%d",monthRange.length];
monthValue = [monthValue stringByAppendingString:rangeString];
toString = [toString stringByAppendingString:monthValue];
NSDate *toDate = [dateFormatter dateFromString:toString];
NSString *finalToDateString = [dateFormatter stringFromDate:toDate];
We are done with From date and To date now form the query using the following statement:
NSString *sqlQuery = [NSString stringWithFormat:@"select * from reminders WHERE Date BETWEEN '%@' AND '%@'",finalFromDateString,finalToDateString];
Thats it,now we must be able to retrieve all the data corresponding to a particular month or the month that is selected in picker view
Thank you all,have a nice time :)
you can use dateFormat for retrieving month from date:
NSDate *today = [NSDate date];
NSDateFormatter *monthFormat = [[NSDateFormatter alloc] init];
[monthFormat setDateFormat:@"MM"];