I'm working on an iOS project, retrieving data from SQLite through JSON. The table has a datetime column called 'date'. When I retrieve the date in Xcode and printout the result, I see "2012-09-02T16:30:00Z". But when I try to convert it using NSDateFormatter, the result is null.
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:#"EdMMM" options:0 locale:[NSLocale currentLocale]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formatString];
NSString *dateString = [dateFormatter stringFromDate:categoryAtIndex.date];
NSLog(#"%# => %#", categoryAtIndex.date, dateString);
The above outputs:
2012-09-02T16:30:00Z => (null)
in stead of a nicely formatted datestring.
Any thoughts?
Thanks to mask8 I found the issue was related to another part of my code. I found this post on stackoverflow to solve the way to change the format of the date that I retrieved through JSON. I also found another post on stackoverflow describing how to handle conversions from a string to NSDate and vice versa.
Related
I need to get objects from parse that not older then a week.
i was trying it with
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEEE, MMMM d yyyy"];
NSDate *date = [object createdAt];
[dateFormatter stringFromDate:date]
Your code is a long way from working and shows some basic misunderstandings. I think you really need to go and learn about Objective-C and Cocoa/UIKit before you jump into Parse. However:
Parse has an automatic column called createdAt on all objects. You should use that.
I'm not sure about your definition of "a week ago" but to get a date exactly one week ago to the second use:
NSDate* date = [NSDate dateWithTimeIntervalSinceNow:-60*60*24*7];
There are more complex ways of doing this if you mean "midnight one week ago" or something like that but I'll leave this as is for the sake of clarity.
Pass this date to a PFQuery using the built in createdAt column:
PFQuery* q = [PFQuery queryWithClassName:[MyClass parseClassName]];
[q whereKey:#"createdAt" greaterThan:date];
Then issue the find:
[q findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
// here objects will contain your PFObject subclasses from the server
}];
I am still kinda new to Objective C. I've searched and searched, applied everything that I've found and I STILL get nul returns.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
[dateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *date = [dateFormat dateFromString:#"08/24/2014"];
And all I get is nul. PLEASE HELP!
Your test string doesn't seem to match your format; if the format is "dd/MM/yyyy" then the string indicates a date on the 8th day of the 24th month of 2014.
Ever time I try to make a string in xcode it won't work can someone point out what's wrong and help please
NSString *date = self.DatePicker.date;
This is the error "Incompatible pointer types initializing 'NSString * strong' with an expression of type 'NSDate *'"
self.DatePicker.date; returns date.
You are storing it in NSString.
Use:
NSDate *date = self.DatePicker.date;
Then if you want to store it in string then use:
NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterFullStyle];
can anyone help me in getting the formatted date from the parsed XML.
I can format that using NSXML formatter but now i am found difficulty in using it through Google XMl parser. where i can get the values through the key but i need that in a formatted way,
return [NSDictionary dictionaryWithObjectsAndKeys:
[[[xmlItem elementsForName:#"title"] objectAtIndex:0] stringValue], #"title",
[[[xmlItem elementsForName:#"link"] objectAtIndex:0] stringValue], #"link",
[[[xmlItem elementsForName:#"pubDate"] objectAtIndex:0] stringValue], #"Date",
nil];
here the pubDate is returned as such from xml with hr,min,sec i need the date alone.
Thanks in advance!!!!
I got the answer
//cell.textLabel.text = [item objectForKey:#"pubDate"];
NSString *yourXMLDate = [item objectForKey:#"pubDate"];
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"E, d LLL yyyy HH:mm:ss Z"];
NSDate *inputDate = [inputFormatter dateFromString:yourXMLDate];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"MMM dd, HH:mm a"];
NSString *outputDate = [outputFormatter stringFromDate:inputDate];
cell.textLabel.text=outputDate;
where this results in Oct 12, 12:30 AM. whatever parser used in the xcode this works fine....
I'm working on an history application so I need to cope with date before and after JC.
I'm trying to parse a string with the form "01/01/-200" but it returns a null date while it's working with "01/01/200".
Here is my code :
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc]init] autorelease];
[dateFormatter setDateFormat:#"dd/MM/y"]; // #TODO Get negative date
[dateFormatter setLenient:NO];
NSDate* date = [dateFormatter dateFromString:dateString];
return date;
I also try using with the form "01/01/200 BC" setDateFormat:#"dd/MM/y G" but I can't make it work neither.
As mvds suggests in his answer, I tried the format "01/01/200 BC" on the simulator, and it's working... the problem only occurs on my iPad (version 3.2.1)
Do you have an idea how to do this properly ?
I just tried this:
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc]init] autorelease];
[dateFormatter setDateFormat:#"dd/MM/y G"];
NSDate *date = [dateFormatter dateFromString:#"01/01/200 BC"];
NSLog(#"refdate %#",[dateFormatter stringFromDate:date]);
date = [date addTimeInterval:24*3600*365*2];
NSLog(#"2 years later %#",[dateFormatter stringFromDate:date]);
which outputs:
refdate 01/01/200 BC
2 years later 01/01/198 BC
This is on 3.2, iPad simulator, so not the most recent SDK, but iPad nonetheless. Do you get different results, running this?
I finally find the trick.
The problem is that my iPad is in French so the Era has a different format :
BC is "av. J.-C."
AD is "ap. J.-C."
So I just had to change my XML file to get the correct format when parsing.
In order to display my date in the AD-BC format, I just convert it afterward :
+ (NSString*) convertIntoBCADString:(NSString*) originalString
{
NSString* newString = [originalString stringByReplacingOccurrencesOfString:#"av. J.-C." withString:#"BC"];
return [newString stringByReplacingOccurrencesOfString:#"ap. J.-C." withString:#"AD"];
}