Save NSDate to CoreData - objective-c

i want to save NSDate from XML to coreData.
I dont know how to set the Value for NSDate. Can someone give me little help?
My Entity attribute is NSDate. Or should it be a String and Save the Date as String??
[Sets setValue:[TBXML textForElement:xmlDate] forKey:#"beginDate"];
Thanks for any response and help,
brush51

Whether you are intentionally obfuscating or not, you don't say it, so I'm going to guess that:
[TBXML textForElement:xmlDate] returns an NSString
beginDate is a Date property of entity Sets
In which case your code is obviously incorrect: you must pass an instance of NSDate to setValue:forKey:. Your code would look like:
NSDateFormatter *myXMLdateReader = [[NSDateFormatter alloc] init];
[myXMLdateReader setDateFormat:#"yyyy-MM-dd"]; // for example
NSDate *itsDate = [myXMLdateReader dateFromString:[TBXML textForElement:xmlDate]];
[myXMLdateReader release];
[Sets setValue:itsDate forKey:#"beginDate"];

It is no problem to add NSDate to CoreData. All you need to do is to convert NSString from XML file to NSDate object. For converting NSString <-> NSDate you can use NSDateFormatter class. In example below you can see how I do this:
NSDateFormatter *parseFormatter = [[[NSDateFormatter alloc] init] autorelease];
[parseFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"US"] autorelease]];
[parseFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSString *dateString = #"Mon, 02 May 2011 21:12:56 +0000";
NSDate *dateToAdd = [parser.parseFormatter dateFromString:dateString];
You can set needful date format and use this code in your project.

Related

Date not displayed properly in the nib file

I have a string with value #"15/11/13". I need to display the same on the label in the nib file.
I am using the following code to display it
NSDateFormatter * df1 = [[NSDateFormatter alloc] init];
[df1 setDateFormat:#"yyyy-mm-dd"];
[df1 setDateStyle:NSDateFormatterShortStyle];
[df1 setTimeStyle:NSDateFormatterNoStyle];
[profile1 setLastPromotionDate:[df1 dateFromString:#"11/11/13"]];
Profile1 is a different class which has lastPromotonDate of type NSDate.
In the nib file I have a outlet to display date which is bound to lastPromotionDate.
When I run the app, the date displayed is Monday, 11 November 2013 12:00:00 AM India Standard Time.
Can I know what is the mistake here? What has to be done so the date displays in this format : 11/11/13
Try this:-
NSString *dateStr=#"11/11/13";
NSDateFormatter *format=[[[NSDateFormatter alloc]init]autorelease];
[format setDateFormat:#"dd/mm/yy"];
NSDate *dt=[format dateFromString:dateStr];
NSString *str=[format stringFromDate:dt];
NSLog(#"%#",str);
NSString *str = #"15/11/13";
// here we create NSDateFormatter object for change the Format of date..
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ;
// here set format of date which is in your output date (means above str with format)
[dateFormatter setDateFormat:#"yy/mm/dd"];
// here you can fetch date from string with define format
NSDate *date = [dateFormatter dateFromString: str];
dateFormatter = [[NSDateFormatter alloc] init] ;
// here set format which you want..
[dateFormatter setDateFormat:#"dd/mm/yy"];
NSString *convertedString = [dateFormatter stringFromDate:date];
//here convert date in NSString
NSLog(#"Converted String : %#",convertedString);
rofile1.text = convertedString;
Why not just set it as:
NSString *str = #"15/11/13";
[label setStringValue: str];
UPDATE:
To convert this string to NSDate of the same format use:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd/mm/yy"];
NSDate * date = [df dateFromString:str];
To get back the string value of date from NSDate use:
NSString *dateStr = [df stringFromDate: date];
Also you may use this dateStr to set you label text as
[label setStringValue: dateStr];
or bind the label to "dateStr". (Do not bind it to "date" of NSDate type, I think this is where you are going wrong).
Use "date" variable that is of NSDate type for your server requests.
UPDATE:
Since you are binding your label to NSDate value, it displays the complete date in the way it is present in NSDate. To retrieve the value of that NSDate in your custom format, you need to use NSDateFormatter that will write the part of NSDate that we need in our format to a NSString.
Also if we convert a NSString to NSDate, it doesn't mean that date formatter will save the NSDate in our custom format, the format of the date formatter specify the format of our string so that NSDate could read the correct date from our custom formatted string. But NSDate will always save the date value in its own format.
We have something called DateFormatter under objects in the library. We can drag and drop that under the text cell if the label in the list view of the nib.
This data formatter converts the date format to string value which is used to display in the UI.

how to get the format 2012-11-19T19:35:00.0000000-07:00 from NSDate?

I have a format 2012-11-19T19:35:00.0000000-07:00 how can I acheive this from NSDate?
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyy-MM-dd'T'HH:mm:ssZ"];
NSDate *today = [NSDate date];
NSString *dateString =[dateFormatter stringFromDate:today];
NSLog(#"Date is: %#",dateString);
I get the NSLog as 2012-11-09T14:22:00+0530
But I want 2012-11-09T19:35:00.0000000-07:00
How can I get this out.Please help me out.
Use
[dateFormatter setDateFormat:#"yyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ"];
More on how to format dates with NSFormater here.
Hope that helps.
NSDate conforms to Unicode Technical Standard, you'll need to look into Date_Format_Patterns to find out the format you want,
According to your requirement, you can do this one. The result will be as
//Your desired : 2012-11-09T19:35:00.0000000-07:00
//You will get : 2012-11-09T12:30:00.0000000+05:30
[dateFormatter setDateFormat:#"yyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ"];

nsformatter dateFromString is nil

Im working on iphone app using xcode,objective c and targeting ios 5 minimum.
All I am trying to do is convert a string to a date. I have read lots on this and it should be a simple straight forward task. I have seen many other questions like this in forum but what is working for people doesnt seem to be working for me.
Here is what I am doing
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString *dobText = [dict valueForKey:#"DateOfBirth"];
NSDate *dobDate = [dateFormatter dateFromString:dobText];
the dobText is always in format #"1999-01-01" which matches the date format set in the date formatter but the result when using date from string is always nil.
can anyone explain this to me and let me know how to fix it?
Look at the user preferences on your device. The documentation says:
Note that although setting a format string (setDateFormat:) in
principle specifies an exact format, in practice it may nevertheless
also be overridden by a user’s preferences—see Data Formatting Guide
for more details.
are you sure the date is using a - and not a – in the data you get from the dict. As i can reproduce a nil result when i use a – (alt + -)
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString *dobText = #"2009-02-12";
NSDate *dobDate = [dateFormatter dateFromString:dobText];
NSLog(#"%#", dobDate);
Try doing this, will likely fix your problem.
NSString *dobText = [[dict valueForKey:#"DateOfBirth"] stringByReplacingOccurrencesOfString:#"—" withString:#"-"];
I suspect like bigkm say, your dashes may be getting in the way.
I would suggest you try a dummy string in line first. e.g.
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString *dobText = #"1999-01-01" // [dict valueForKey:#"DateOfBirth"];
NSDate *dobDate = [dateFormatter dateFromString:dobText];
Now does that work? It should. Now triangulate another way:
Remove the dashes completely from the dob text, so that it has the format 'yyyyMMdd'
Have the date formatter look for this same format
Does that work? It should. And that would prove that the separator characters in your format are the issue and need some further inspection (or cleansing as bigkm suggested).
Side node re threading: NSDateFormatter is fine if you use it on the thread on which it was created. If you don't, you'll know b/c the app will crash.
If your date format is correct, I can suggest to set NSLocale to your NSDateFormatter.
For me this code works on simulator:
NSString *format = [NSString stringWithFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:format];
NSDate *date = [df dateFromString:pubDateSttring];
but on the device date is always NIL.
I've found workaround of setting NSLocale:
df.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];

How can I Convert a NSString to a valid NSDate (iOS)

I'm passing a string from javascript to Objective-C in the form of "2012-02-17 14:21:30 +0000".
My code is as follows:
NSString *firingDate = [_parameters objectForKey:#"fire"];
NSDate *notificationDate = [NSDate dateWithString:firingDate];
The issue is that I ended up reading the OS X reference instead of the iOS docs (doh!) so this throws a warning as dateWithString isn't present in iOS. In theory I suppose that this shouldn't work at all but it does, albeit with that warning.
What is the Correct way to convert the string to a NSDate?
The correct way is to use NSDateFormatter as a factory to create dates from strings (and vice versa).
Try:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"YYYY-MM-dd HH:mm:ss Z";
NSDate *notificationDate = [formatter dateFromString:firingDate];
Try this:
NSString *firingDate = [_parameters objectForKey:#"fire"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *notificationDate = [dateFormatter dateFromString:firingDate];
Check out the reference for parsing dates from multiple regions.
Don't forget to release your formatter when finished.

How to turn a NSString into NSDate?

Ive been racking my brains with no luck. Could someone please tell me how i would convert this string:
"2011-01-13T17:00:00+11:00"
into a NSDate?
The unicode date format doc is here
Also, for your situation, you could try this:
// original string
NSString *str = [NSString stringWithFormat:#"2011-01-13T17:00:00+11:00"];
// convert to date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// ignore +11 and use timezone name instead of seconds from gmt
[dateFormat setDateFormat:#"YYYY-MM-dd'T'HH:mm:ss'+11:00'"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Australia/Melbourne"]];
NSDate *dte = [dateFormat dateFromString:str];
NSLog(#"Date: %#", dte);
// back to string
NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
[dateFormat2 setDateFormat:#"YYYY-MM-dd'T'HH:mm:ssZZZ"];
[dateFormat2 setTimeZone:[NSTimeZone timeZoneWithName:#"Australia/Melbourne"]];
NSString *dateString = [dateFormat2 stringFromDate:dte];
NSLog(#"DateString: %#", dateString);
[dateFormat release];
[dateFormat2 release];
Hope this helps.
put the T part in single quotes, and check the unicode docs for the exact formatting. In my case, I have something similar, which I do this:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd'T'HH:mm:ss.SSS"];
Again, not exactly the same, but you get the idea. Also, be careful of the timezones when converting back and forth between strings and nsdates.
Again, in my case, I use:
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"America/New_York"]];
Did you try this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *dateT = [dateFormatter dateFromString:str];
Cheers
You might check out TouchTime.
https://github.com/jheising/TouchTime.
It's a direct port of the awesome strtotime function in PHP in 5.4 for Cocoa and iOS. It will take in pretty much any arbitrary format of date or time string and convert it to an NSDate.
Hope it works, and enjoy!
Try using this cocoapods enabled project. There are many added functions that will probably be needed as well.
"A category to extend Cocoa's NSDate class with some convenience functions."
https://github.com/billymeltdown/nsdate-helper
Here's an example from their page:
NSDate *date = [NSDate dateFromString:#"2009-03-01 12:15:23"];