iOS: DatePicker selector shows exact value but it returns wrong date - ios7

I create a date picker programmatically for ios 7 and set maximum and minimum date.But it return wrong year for some particular december days.
[fromDatePicker addTarget:self action:#selector(fromDatePickerAction:) forControlEvents:UIControlEventValueChanged];
NSDate *date = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents * components = [[NSDateComponents alloc] init];
NSDateComponents *monthComponents = [gregorian components:NSMonthCalendarUnit fromDate:date];
int currentMonth = [monthComponents month];
NSDateComponents *dayComponents = [gregorian components:NSDayCalendarUnit fromDate:date];
int dayss=[dayComponents day];
[components setYear:0];
NSDate * minDate = [gregorian dateByAddingComponents: components toDate: date options: 0];
[components setYear:0];
[components setMonth:12-currentMonth];
[components setDay:31-dayss];
NSDate * maxDate = [gregorian dateByAddingComponents: components toDate: date options: 0];
fromDatePicker.minimumDate=minDate;
fromDatePicker.maximumDate=maxDate;
[self.view addSubview:fromDatePicker];
- (void) fromDatePickerAction:(id)sender{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY-MM-dd hh:mm aa"];
[self.delegate didTouchAddNotePicker:[dateFormatter stringFromDate:[fromDatePicker date]]];
NSLog(#"%#",[dateFormatter stringFromDate:[fromDatePicker date]]);
//_lblDate.text=[dateFormatter stringFromDate:[fromDatePicker date]];
}
Here I choose this date shown in figure 1.But it returns the date in figure 2
Please help to solve this problem

This problem is solved by changing [dateFormatter setDateFormat:#"YYYY-MM-dd hh:mm aa"] to [dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm aa"] in date picker selector method
Please click this link to know about the difference between YYYY and yyyy

Related

How to get absolute value from NSDateComponents

I want to take absolute value from NSDateComponents.
<NSDateComponents: 0x600000477c80>
Calendar Year: -1
I tried the following way.But I did not get the exact value.How can i get the value 1 from the components.
NSDate *startDate = [formatter dateFromString:birthDate];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitYear
fromDate:startDate
toDate:today
options:0];
NSInteger year = [components year];
If you want to get difference of the dates in the years, then you have to use method : calendar: fromDate: toDate:
And if you want to get only year from the date, then you have to use this method : calendar: fromDate:.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
NSDate *today = [NSDate date];
NSString *birthDate = #"04/06/1984 01:45:20";
NSDate *startDate = [formatter dateFromString:birthDate];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
//NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitYear fromDate:startDate toDate:today options:0]; //Option 1
NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitYear fromDate:startDate]; // Option 2
NSInteger year = [components year];
NSLog(#"Years : %ld", year);
If you use method of option 1, then it will print Years : 35.
And if you use method of option 2, then it will give Years : 1984.

I have a method that takes timezone name as a parameter and gives the current date's day component. But it gives different incorrect results

Suppose i have two parameters
Australia/Sydney (GMT+10) offset 36000 &
Asia/Kolkata (IST) offset 19800
as timezones, it gives different values for the day component of current day itself. I am not able to figure out what am i doing wrong.. :( A little clue will be appreciated
-(NSInteger)getDayWithTimeZoneName:(NSString *)timeZoneName{
// Instantiate a timezone
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:timeZoneName];
//Instantiate a date formatter
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setLocale:[NSLocale currentLocale]];
[df setTimeZone:timeZone];
[df setDateFormat:#"yyyy-MM-dd"];
NSString *dateStr = [df stringFromDate:[NSDate date]];
NSDate *date = [df dateFromString:dateStr];
// NSDateComponents *components = [[NSCalendar currentCalendar] components: NSCalendarUnitDay fromDate:date];
NSDateComponents *components = [[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]components:NSDayCalendarUnit fromDate:date];
NSInteger day = [components day];
NSLog(#"Date component for timeZone %# is %ld", timeZoneName,(long)day);
return day;
}
NSFormatter isn't designed to convert or calculate dates:
NSString *dateStr = [df stringFromDate:[NSDate date]];
dateStr is the date in Sydney (2016-05-16 0:00).
NSDate *date = [df dateFromString:dateStr];
date is GMT, 10 hours behind Sydney (2016-05-15 14:00).
Use NSCalendar to calculate dates:
NSCalendar *calendar = [NSCalendar currentCalendar];
calendar.timeZone = timeZone;
NSDateComponents *components = [calendar components:NSCalendarUnitDay fromDate:[NSDate date]];
NSInteger day = [components day];

Calculate NSDate for day after summertime

I just found a but in one of my apps; the problem is that I am calculating the days in a menu using initWithTimeIntervalSinceNow and adding the caculation (day*X) (X meaning the day in question).
However, the night between 27th and 28th of oct the CEST timezone will become CET thus meaning that for one day the time should be calculated by (day*X)+3600. However, I do not want to use if cases and believe there should be a better way of dealing with this.
How can I calculate future days with keeping summer/winter time in mind?
My code:
int day = (60*60*24);
NSDate *today = [NSDate date];
NSDate *days1 = [[NSDate alloc] initWithTimeIntervalSinceNow:day];
NSDate *days2 = [[NSDate alloc] initWithTimeIntervalSinceNow:(day*2)];
NSDate *days3 = [[NSDate alloc] initWithTimeIntervalSinceNow:(day*3)];
NSDate *days4 = [[NSDate alloc] initWithTimeIntervalSinceNow:(day*4)];
NSDate *days5 = [[NSDate alloc] initWithTimeIntervalSinceNow:(day*5)];
NSDate *days6 = [[NSDate alloc] initWithTimeIntervalSinceNow:(day*6)];
NSDate *days7 = [[NSDate alloc] initWithTimeIntervalSinceNow:(day*7)];
Try this:
- (NSDate *)dateByAddingXDaysFromToday:(int)days
{
NSDateComponents *comps = [[NSDateComponents alloc] init];
comps.day = days;
return [[NSCalendar currentCalendar] dateByAddingComponents:comps toDate:[NSDate date] options:0];
}
Then you can use:
NSDate *days1 = [self dateByAddingXDaysFromToday:1];
//etc...
It's not clear what you are trying to achieve with calculating the date objects. I'm assuming you want to create objects that refer the next seven days, same time as now.
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:1];
NSDate *now = [NSDate date];
NSDate *days1 = [calendar dateByAddingComponents:components toDate:now options:0];
NSDate *days2 = [calendar dateByAddingComponents:components toDate:days1 options:0];
NSDate *days3 = [calendar dateByAddingComponents:components toDate:days2 options:0];
....

Not removing the Time Components from NSDate

I need to remove the time components from NSDate for instance: I have the value of '2012-08-12 22:15:54 +00000' but I got the value '2012-08-12 03:00:00 +00000'
The Code I am Using is:
NSDate* getNDateOfWeek(int week, int day) {
NSDate *now = [NSDate date];
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
calendar.timeZone = [NSTimeZone systemTimeZone];
unsigned unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit;
NSDateComponents *comp = [calendar components:unitFlags fromDate:now];
[comp setWeekday:day];
[comp setWeek: week];
[comp setHour:0];
NSDate *resultDate = [calendar dateFromComponents:comp];
return resultDate;
}
Any help?
You might be trying to achieve a different result but if you are simply looking for 'todays' date then what about something like:
// Output -> 'Mon, Jan 16, 2012'
- (NSString *)todaysDate
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE MMM d Y"];
return [dateFormat stringFromDate:[[NSDate alloc] init]];
}

How to get today's date in the Gregorian format when phone calendar is non-Gregorian?

NSDate *now = [[NSDate alloc] init];
gives the current date.
However if the phone calendar is not Gregorian (on the emulator there is also Japanese and Buddhist), the current date will not be Gregorian.
The question now is how to convert to a Gregorian date or make sure it will be in the Gregorian format from the beginning. This is crucial for some server communication.
Thank you!
NSDate just represents a point in time and has no format in itself.
To format an NSDate to e.g. a string, you should use NSDateFormatter. It has a calendar property, and if you set this property to an instance of a Gregorian calendar, the outputted format will match a Gregorian style.
NSDate *now = [NSDate date];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setCalendar:gregorianCalendar];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setTimeStyle:NSDateFormatterFullStyle];
NSString *formattedDate = [formatter stringFromDate:now];
NSLog(#"%#", formattedDate);
[gregorianCalendar release];
[formatter release];
The picked answer actually I can't compare them. only display is not enough for my project.
I finally come up with a solution that covert (NSDate) currentDate -> gregorianDate
then we can compare those NSDates.
Just remember that the NSDates should be used temporary .(it do not attach with any calendar)
NSDate* currentDate = [NSDate date];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *gregorianComponents = [gregorianCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:currentDate];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:[gregorianComponents day]];
[comps setMonth:[gregorianComponents month]];
[comps setYear:[gregorianComponents year]];
[comps setHour:[gregorianComponents hour]];
[comps setMinute:[gregorianComponents minute]];
[comps setSecond:[gregorianComponents second]];
NSCalendar *currentCalendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *today = [currentCalendar dateFromComponents:comps];