3

I have read date from XML which give me back string where the date is like DD-MM-YYYY. But when I want to add it to my core data database, SQLite sorts me that wrong so I have to convert it to date format or to string like: YYYY-MM-DD. But SQLite does not support either Date() or To_Date() functions. Can you help me?

Gumbo
  • 643,351
  • 109
  • 780
  • 844
Csabi
  • 3,097
  • 17
  • 59
  • 107

2 Answers2

9

You can use NSDateFormatter to format a date in Objective C.

Here's a quick example which might not do exactly what you want, but should hopefully give some pointers:

// Convert my dd-mm-yyyyy string into a date object
NSString *stringDate = @"01-02-2011";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];

NSDate *date = [dateFormatter dateFromString:stringDate];

// Convert my date object into yyyy-mm-dd string object
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *formattedStringDate = [dateFormatter stringFromDate:date];

[dateFormatter release];

Hope this helps!

Nick.

Douwe Maan
  • 6,888
  • 2
  • 34
  • 35
Nick Cartwright
  • 8,334
  • 15
  • 45
  • 56
  • Will it work for my example? when i have to use dateFromString and the string is DD-MM-YYYY? – Csabi Feb 02 '11 at 09:36
  • Good question! I've posted an update to my post. I've not tested this code, but hopefully it gives the idea! Best of luck. N – Nick Cartwright Feb 02 '11 at 09:41
  • NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd-MM-yyyy"]; NSDate *formattedDate = [dateFormatter dateFromString:string]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; NSString *elmentendo = [dateFormatter stringFromDate:formattedDate]; – Csabi Feb 02 '11 at 09:42
  • Are you sure your DD-MM-YYYY date is in the `string` variable? – Douwe Maan Feb 02 '11 at 09:45
  • I gues it does not support DD-MM-YYYY format because it throw s back nil if I run it after NSDate *date = [dateFormatter dateFromString:stringDate]; – Csabi Feb 02 '11 at 09:48
  • Yes - this is great! I just checked and it seems to work fine on my machine. What output do you get on your machine when you print out elmentendo to the terminal? – Nick Cartwright Feb 02 '11 at 09:48
  • Yes Douwe I get string right from function - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { So I work with string – Csabi Feb 02 '11 at 09:49
  • Perhaps this could help: http://stackoverflow.com/questions/3094819/nsdateformatter-returning-nil-in-os-4-0 – Nick Cartwright Feb 02 '11 at 09:50
  • Nick Your answer was right and worked perfectly for date like @"23-01-1989" but I have date in format @"23.01.1989" i just have to solve how to convert that maybe i write for that a function once more thank – Csabi Feb 02 '11 at 10:15
1

Using Nick's tutorial and answer the solution is :

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd.MM.yyyy"];

        NSDate *date = [dateFormatter dateFromString:@"01.02.1989"];

        NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
        // Convert my date object into yyyy-mm-dd string object
        [dateFormatter1 setDateFormat:@"yyyy-MM-dd"];
        NSString *formattedStringDate = [dateFormatter1 stringFromDate:date];

        [dateFormatter release];
        [dateFormatter1 release];
        NSLog(formattedStringDate);

        objektObrat.dateOfObrat = [[NSString alloc] initWithString:formattedStringDate];
Csabi
  • 3,097
  • 17
  • 59
  • 107