-2

Simple question, annoying problem. I have a date picker with which I select a time (say 11 AM). Then a button that's connected to a method that prints that time to the console. Literally just a five line project. The time printed to the console shows an hour earlier to whatever time I set (so, if I set 11 AM, it prints 10 AM an so on). The time zone on my mac is set correctly, the clock displays the correct time. I have looked on Stack Overflow and have seen some very long answers that involve NSCalendar and components etc... but I am pretty sure this should be a one line thing. Please help before I proceed to set my mac alight. Thanks

---------------------------------------------------------------EDIT--------------------------------------------------------------

Ok, I'll post some code to see if it makes it clearer:

- (IBAction)setReminder:(id)sender{

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSString *dateString = [formatter stringFromDate:self.datePicker.date];


    NSLog(@"Setting a reminder for: %@", dateString);

    NSDate *date = [formatter dateFromString:dateString];

    NSLog(@"another proof that date is right: %@", date);

    UILocalNotification *note = [[UILocalNotification alloc]init];
    note.alertBody = @"This is a reminder!";
    note.fireDate = date;

    [[UIApplication sharedApplication] scheduleLocalNotification:note];}

The first NSLog returns the right time, but the second NSLog returns an hour earlier that the hour set up using the datepicker, so the notification is never triggered. What am I doing wrong? tx

Paul
  • 1,277
  • 5
  • 28
  • 56

2 Answers2

0

Date Picker always return Date in UTC.

create a dateformatter and get string it will show expected time.

jagdish
  • 166
  • 5
-1

Please Use this it will be help you out

NSDateFormatter *formattor=[[NSDateFormatter alloc]init];
formattor.dateFormat=@"yyyy-MM-dd HH:mm:ss";
NSString *date_string=[formattor stringFromDate:self.datePicker.date];
NSLog(@"%@",date_string);
Ajharudeen khan
  • 246
  • 2
  • 10
  • this is a good start, which I had tried, but the date itself is still an hour earlier. Pz look at the edit. – Paul Jul 16 '16 at 16:31