0

I have a NSDate which is not being correctly formatted.

I have declared a UITextField in the .h :

@property (weak, nonatomic) IBOutlet UITextField *datetimeTextField;

Then I have a 3rd party UIPicker that picks a Date and inserts it in the mentioned TextField:

// Method to avoid diplaying the keyboard. 
    -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
        ActionSheetDatePicker *datePicker = [[ActionSheetDatePicker alloc]initWithTitle:@"Select Date and Time" datePickerMode:UIDatePickerModeDateAndTime selectedDate:[NSDate date] doneBlock:^(ActionSheetDatePicker *picker, id selectedDate, id origin) {

        // As you can see here it's taking the correct (non-formatted) date
        NSLog(@"Selected %@", selectedDate); // VALUE = Sat Nov 10 10:00:41 2018

        //create a date formatter
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

        //set its format as the required output format
       [formatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];

        //get the output date as string
        NSString *selectedDateString = [formatter stringFromDate:selectedDate];

        self.datetimeTextField.text = selectedDateString;

        // And here I get the value I want to store in Parse stored in datetimeTextField.text

        NSLog(@"Selected After reformat %@", self.datetimeTextField.text); // VALUE = 10-11-2018 00:35:06

    } cancelBlock:^(ActionSheetDatePicker *picker) {

    } origin:self.view];
    datePicker.minuteInterval = 5;
    [datePicker showActionSheetPicker];

 return NO;
 }

My problem starts when I have to call an IBAction to store this NSDate in my Parse Cloud (I have a Date column that would only accept NSDate.

- (IBAction)createeventAction:(id)sender{

   // Here I "catch" the value previously stored from the Picker. 
   NSString *dateString =  datetimeTextField.text; //// 07-11-2018 22:00:42 (correct format)

   // Here I convert the NSString into NSDate with the same formatting
   NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
   [dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
   NSDate *date = [dateFormatter dateFromString:dateString];

   // But for some reason, date prints incorrectly. 
   NSLog(@"DATE in here ====>>> %@", date); // Sat Nov 10 10:00:41 2018

 }

Problem:

I would like to convert a NSString (datetimeTextField.text) to a NSDate without losing the format.

EDIT 1:

I had consulted the accepted answer from this question How to convert NSStrings to NSDate but for some reason, it does not work for me.

EDIT 2:

To make it more clear:

Code to convert NSDate to NSString.

// We have a date (not formatted) => Sat Nov 10 10:00:41 2018 
//create a date formatter
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
NSString *selectedDateString = [formatter stringFromDate:selectedDate];
self.datetimeTextField.text = selectedDateString;
// Date formatted => 10-11-2018 00:35:06

Code to convert NSString back to NSDate:

NSString *dateString =  datetimeTextField.text; // 10-11-2018 10:00:41 (correct format)
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(@"DATE in here ====>>> %@", date); // Sat Nov 10 10:00:41 2018 (not formatted. WHY?)
  • 2
    Which format did expect? NSLog always use the description method of the class. Description is always formatted Sat Nov 10 00:35:06 2018 – ivion Nov 03 '18 at 20:16
  • I expected to have dd-MM-yyyy HH:mm:ss. –  Nov 03 '18 at 20:22
  • 1
    Then you have to use NSLog(@"%@", [dateFormatter stringFromDate:date]); – ivion Nov 03 '18 at 20:27
  • but does that return a NSString or a NSDate Object? –  Nov 03 '18 at 20:35
  • Then you have a NSString again. date is of type NSDate. That should be accepted by your Parse Cloud. – ivion Nov 03 '18 at 20:42
  • Well thats the thing. I need to have a NSDate because I am storing dates. –  Nov 03 '18 at 20:47
  • You created it here: NSDate *date = [dateFormatter dateFromString:dateString]; Or did you need in other part of the code. – ivion Nov 03 '18 at 20:51
  • Yes. But "date", which is "Sat Nov 10 00:35:06 2018" should be formatted using "dd-MM-yyyy HH:mm:ss" declared a few lines above. Thats the problem, a formatting problem. –  Nov 03 '18 at 20:56
  • I think it is a misunderstanding NSDate has just a value not a format. The value is correct. – ivion Nov 03 '18 at 20:59
  • Your code is fine. There is nothing to change. The line `NSDate *date = [dateFormatter dateFromString:dateString];` gives you what you want. `NSDate` has no format. Ignore the format shown when you log the date, it's irrelevant. – rmaddy Nov 04 '18 at 00:11

2 Answers2

1

Well thats the thing. I need to have a NSDate because I am storing dates.

You seem to misunderstand the difference between NSDate and what you get back from NSDateFormatter. NSDate is just a class that stores a date in no particular format -- it stores the information inside the object. If you want to display the date in a particular format, you need to create a string from the date, and you use NSDateFormatter to do convert your date into a string that expresses the date in the format you need.

If you print the date to the console using NSLog(), like:

NSLog("My date is %@", myDate);

then NSLog will just use the date's description method, which gives you a sort of default expression of the date. If you want to log the date in some specific format, you'll need to set up a date formatter with that format and then use it:

NSLog("My formatted date is %@", [myFormatter stringFromDate:myDate]);
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Hi caleb! But I don't need to convert date to string, but the opposite!. I already have a NSString (datetimeTextField.text) formatted correctly (07-11-2018 22:00:42). My problem is to convert that NSString into a NSDate :) –  Nov 03 '18 at 21:10
  • Ah, I see. I thought you were unhappy with the format in which it's being logged. What time zone are you in? – Caleb Nov 03 '18 at 21:15
  • I am in GMT+1 :) –  Nov 03 '18 at 21:17
  • It's hard to see how input of `07-11-2018 22:00:42` results in a date of `Sat Nov 10 00:35:06 2018`. The 1-hour time zone difference from UTC explains part of the 2 hr 35 min difference, and daylight savings might explain another hour, but hard to see where the 35 minutes comes from. – Caleb Nov 03 '18 at 21:21
  • Oh sorry the values are the same. Just they are different examples. I just want to convert a NSString to NSDate. Thats all. –  Nov 03 '18 at 21:25
0

In the end it turned out that my code was fine. Only thing is that I did not notice that the Date was declared as String in Parse instead of as Date.