NSDateFormatter not working for me? - nsstring

Apparently "2013-02-27T11:01:00.000" isn't a valid date, because whenever I use the NSDateFormatter to edit a date in that format to just the time with AM/PM I get nothing.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
NSString *time = [formatter stringFromDate:DepartTimeDate];
Does this mean I have to find a way to edit the date manually, if so, does anyone have any clue how this could be done?
Example: 2013-02-27T11:01:00.000 the "11:01" is all that I need, is there anyway to separate this from the rest of the "date", or string? Also, it is 24 hour time, so 1PM would be "2013-02-27T13:01:00.000"
If anyone has any clues, I'd be very thankful!

Using some tricks , I have come up with following solution for your question.
NSString *departTimeDate = #"2013-02-27T11:01:00.000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS"];
NSDate *date = [dateFormatter dateFromString:departTimeDate];
[dateFormatter setDateFormat:#"HH:mm"];
NSLog(#"Expected Result___ %#",[dateFormatter stringFromDate:date]);
also please look at Date Formatting Guide.
Hope that this can give you a clue.

Related

Why is moment JS subtracting or adding days not working?

I'm trying to use Moment JS and having issues adding or subtracting dates.
For example, I'm trying to sanity check right now and have this in code:
let someDateString = "01/01/2000 12:00 AM";
let startMoment = moment(someDateString, "MM/DD/YYYY hh:mm:ss A");
let futureMoment = startMoment.subtract(1, "day");
^ this doesn't work and when I print future and start moment, they show the same time and date.
I tried also changing stuff around so that I create a new moment instead of using startMoment:
let someDateString = "01/01/2000 12:00 AM";
let startMoment = moment(someDateString, "MM/DD/YYYY hh:mm:ss A");
let futureMoment = moment(someDateString, "MM/DD/YYYY hh:mm:ss A").subtract(1, "day");
But it's still not working. Am I just missing something?
Help would be greatly appreciated as I am new to this and have wracking my head for a while on why it's not working.
Watch the documentation closely, substracting or adding dates forces you to specify the chrono unit. You just wrote 'day', but its either 'days' or just simply 'd'.
let someDateString = "01/01/2000 12:00 AM";
let startMoment = moment(someDateString, "MM/DD/YYYY hh:mm:ss A");
let futureMoment = startMoment.subtract(1, "days");
will work.

NSDateformatter returns wrong date from an asp.NET date

I am using restkit to map my JSON into coredate. In this JSON I find ASP.Net dates. When I try to map them in core date I sometimes see that the date is one day earlier.
Date: "/Date(1389740400000+0100)/"
This is how I add a defaultDateFormatter in my code.
RKDotNetDateFormatter *dateFormatter = [RKDotNetDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss ZZZZ"];
dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[RKEntityMapping addDefaultDateFormatter:dateFormatter];
Can somebody help me with this ?
Try
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
I suspect you're running into GMT discrepancies. Try using the systemTimeZone instead:
dateFormatter.timeZone = [NSTimeZone systemTimeZone];

stringByAddingPercentEscapesUsingEncoding adds unexpected characters?

I'm having a hard time getting my NSURL to work, when I create the final string before converting to URL it adds unwanted character to the end of the string, why is this happening and how can I fix it?
Here is my code:
NSString *remotepathstring = [[NSString alloc] init];
remotepathstring=newdata.remotepath;
NSLog(#"remotepathstring = %#",remotepathstring);
NSString *remotepathstringwithescapes = [remotepathstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"remotepathstring = %#",remotepathstringwithescapes);
remotepathURL =[NSURL URLWithString:remotepathstringwithescapes];
NSLog(#"RemotePathUrl=%#",remotepathURL);
Log outputs as follows:
"remotepathstring = http://nalahandthepinktiger.com/wp-content/uploads/nalah-sheet-5.pdf‎"
"remotepathstring = http://nalahandthepinktiger.com/wp-content/uploads/nalah-sheet-5.pdf%E2%80%8E"
"RemotePathUrl=http://nalahandthepinktiger.com/wp-content/uploads/nalah-sheet-5.pdf%E2%80%8E"
The sequence %E2%80%8E is a Unicode LEFT-TO-RIGHT MARK. This is present in your original remotepathstring, but invisible when printed out via NSLog.
The question becomes: how does newdata.remotepath get populated in the first place? Somewhere along the line it sounds like you need to perform some extra cleanup of input strings to strip out such a character.
Unrelated to the core question, it would seem you're a newcomer to Objective-C. This code is redundant and wasteful:
NSString *remotepathstring = [[NSString alloc] init];
remotepathstring=newdata.remotepath;
You create a string, only to immediately throw it away and replace it with another. If you're not using ARC, this has the additional problem of leaking! Instead do:
NSString *remotepathstring = newdata.remotepath;

xcode 4, Thread 1:Program received signal:"EXC_BAD_ACCESS."

When i added the code(textField.text=preTotalNumber;) in the method(onButtonPressed),the failure appeared.
Can anyone please help me?thk
preTotalNumber should be an NSString, and if not you should convert it to NSString. for instance if it's an integer you can use the following:
[NSString stringWithFormat:#"%d", preTotalNumber];

iPhone CoreData join

this is my core data model:
I'm trying to get all LanguageEntries from a database for a given category.categoryName and languageset.languageSetName e.g.
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"LanguageEntry" inManagedObjectContext:del.managedObjectContext];
[fetchRequest setEntity:entity];
NSString* predicateString = [NSString stringWithFormat:#"Category.categoryName = %# AND LanguageSet.languageSetName = %#",
#"Food", #"English####Spanish"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:predicateString];
NSError *error = nil;
NSArray* objects = [del.managedObjectContext executeFetchRequest:fetchRequest error:&error];
This always returns 0 objects. If I set the predicate string to match on one relationship (e.g. Category.categoryName = Food or languageSet.languageSetName = English####Spanish) it will return data.
This is baffling, can anyone shed some light?
->Ken
Your can't think of Core Data as SQL. There is no such thing as a "join" in Core Data. In this case, your trying to find the intersection of two sets of objects. Close to a join logically but not in the implementation details. And the programming devil is always in the details.
Try using the logical equal "==" like so:
#"Category.categoryName == %# AND LanguageSet.languageSetName == %#"
I believe that will solve your problem.
The data model has a predicate editor hidden way in it. It can help you set up predicates even if you don't embed them in fetches in model itself. Just select an entity, in your case "LanguageEntity", then add a Fetch Request. The edit predicate will appear and you can use the dialog to create the predicate. It will display a textual version of the predicate that you can copy into your code.
The predicate properly wasn't created correctly, you must pass the parameters to predicateWithFormat. It should have been:
fetchRequest.predicate = [NSPredicate predicateWithFormat:#"Category.categoryName = %# AND LanguageSet.languageSetName = %#",
#"Food",
#"English####Spanish"];
In case you were wondering what that does is it puts quotes around the string parameters automatically which are required when using a string in a predicate. When you created the query using NSString's format method that did not put the required quotes in.

Resources