How to convert a DD-MM-YYYY date format string into a YYYY-MM-DD date format string or into a NSDate object in Objective-C? - objective-c

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?

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.

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];

Related

Time formats for NSDate

I have datetime values coming into my app from a web service. The format is as follows:
2013-03-24T09:45:00.000-04:00
I need only the time from this value. I am using NSDateFormatter and am setting the date format as follows:
[df setDateFormat:#"HH:mm:ss"];
and when I do this I get a rather cryptic runtime error. However, I have no errors when I use the following (though it doesn't satisfy my requirement):
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.sssz"];
Can someone help?
Try this
NSString *originalDateString = #"2013-03-24T09:45:00.000-04:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.sssz"];
NSDate *date = [dateFormatter dateFromString:originalDateString];
[dateFormatter setDateFormat:#"HH:mm:ss"];
NSString *newDateString = [dateFormatter stringFromDate:date];
Maybe if you use date formater to retrieve a NSDate instance and from there do you own formating.
NSSTring *dateString = "2013-03-24T09:45:00.000-04:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *dateFromWebApp = [dateFormatter dateFromString:dateString];
Haven't tested tho but it should work.
Cheers

Turning NSDate into NSString [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert NSDate to NSString
convert string to nsdate
Currently I have this code. It's for adding events to the calendar.
[...]
event.startDate = [[NSDate date] dateByAddingTimeInterval:86400];
event.endDate = [[NSDate date] dateByAddingTimeInterval:90000];
What I need is the code to add to a spesific start date and end date, and that's where NSString comes in handy. But I've had no luck converting it so far.
Refer this code :
NSString to NSDate
NSString *dateString = #"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDate convert to NSString:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#", strDate);
[dateFormatter release];
Hope it helps you
convert NSDate to NSString as
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSString *string = [dateFormatter stringFromDate:date];
[dateFormatter release];
That's an example if you need the format like “Nov 23, 1937”:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateStyle= NSDateFormatterMediumStyle;
NSString* string= [formatter stringFromDate: date];
Check out the reference for other styles. If you need another style that hasn't a constant, you can use the date format, in this case it's:
formatter.dateFormat= #"MMM dd, yyyy"; // same as medium style
But the preferred way is to use the style, use the format only if there isn't a propert style.

Convert String to NSDate always nil

I want to covert from a NSString to NSDate object.
My string is : #"01/01/2013.
Here is the code to convert the string to NSDate:
- (NSDate*) formatDateTimeFromString: (NSString* ) string{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate* date= [dateFormatter dateFromString:string ];
return date;
}
I debug into the function and see that the date object is always nil. Did I have any mistake?
Your date formatter is wrong (might be hard to see at first)
Change
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
to
[dateFormatter setDateFormat:#"dd/MM/yyyy"];

NSString to NSDate conversion for a specific format

I receive NSString(s) in the following format:
2013-05-18T17:37:06.9419518Z
How do I convert the date into a NSDate properly so I can compare it with another NSDate that contains the current date/time
This is what I have right now:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZ"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:extractString];
NSLog (#"dateFromString: %#", dateFromString);
[dateFormatter release];
[dateFromString release];
but I get a (null) output in the log out of it
thank you.
If the 'Z' is a constant at the end of your time, then you need to put it in single quotes in the date format.
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSSSSS'Z'"];

Convert RSS date format to german date in Xcode

I have a little App which shows data from an RSS feed. Because it's date format is very long and now well for being printed out I want to convert it to the German's date format. This code I have now, but it always return "nil":
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormat setDateFormat:#"dd MMM, yyyy"];
NSString *contentDate = [dateFormat stringFromDate:date];
What's wrong with it? Thanks for answers!
RSS dates are a bit tricky.. you can grab the NSDate+InternetDateTime.h/.m from here:
https://github.com/mwaterfall/MWFeedParser
that should take care of the hard work.