Steaphann Steaphann -5 years ago 122
Objective-C Question

UIDatepicker date one day earlier after going below or above min or max date

I have made a custom datePickerView with a done and cancel button.

- (id)initWithCoder:(NSCoder *)aDecoder{
if (self = [super initWithCoder:aDecoder]) {
return self;

-(void)setupWithMinimumDate:(NSDate *)minimumDate andMaximumDate:(NSDate *)maximumDate andTag:(NSInteger)tag{
viewToolbar.backgroundColor = [UIColor toolbarBackgroundColor];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.timeZone = [NSTimeZone localTimeZone];
datePicker.locale = [NSLocale currentLocale];

datePicker.minimumDate = minimumDate;
datePicker.maximumDate = maximumDate;
tagValue = tag;

[self.delegationListener andTag:tagValue];

[self.delegationListener didTapCancel];

-(IBAction)datePickerValueChanged:(UIDatePicker *)sender{
NSLog(@"DATE = %@",; =;

You see the Log statement in
. It gives me back the correct date when I keep inside my minimum and maximum date. But when I scroll above or below my min or max date it always returns me a date which is one day earlier than the date selected.

I know that when I format the date with
it gives me back the correct Date string. But here is the problem, I store the date that I receive from my datePicker on a Realm object.

Now that realm object I sent to a webserver that needs a dateformatting with UTC timezone. So I always sent a date which is one day earlier.

I hope it's a little bit clear, if not please ask !

Answer Source

as i check your code you are NSLog of NSDate object but NSDate log is not sure to return correct NSDate because NSDate returns current time in GMT time zone so following is the code to do bit calculation of GTM timezone to your local timezone:

-(IBAction)datePickerValueChanged:(UIDatePicker *)sender{
    NSTimeZone* GTMTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    NSTimeZone* SystemTimeZone = [NSTimeZone systemTimeZone];

    NSInteger GTMOffset = [GTMTimeZone];
    NSInteger Systemoffset = [SystemTimeZone];
    NSTimeInterval interval = Systemoffset - GTMOffset;

    NSDate* correctDate = [[NSDate alloc] initWithTimeInterval:interval];
    NSLog(@"DATE = %@",correctDate); = correctDate;

i was implicated in my project from here

Other way is you can convert your NSDate in to string with correct dateformate and nslog it that will be show you correct log like following:

-(IBAction)datePickerValueChanged:(UIDatePicker *)sender{

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSLog(@"Todays date is %@",[formatter]); =;

so you can store the date as a string and send it to your backend hope that help to you

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download