Detaching a string into two using objective c [duplicate] - objective-c

This question already has answers here:
Separate 1 NSString into two NSStrings by whiteSpace
(2 answers)
Closed 10 years ago.
I am getting the date from data base in this format, '01/02/2013 17:00'. I have to detach the date and time and put it in separate columns in the nstableview (date in date column and time in time column). How should i do this?

You can:
NSArray *dateAndTime = [#"01/02/2013 17:00" componentsSeparatedByString:#" "];
This if you are certain about that format. Otherwise you should be careful and rely on something like NSDateFormatter.

NSString *dateTime = #"01/02/2013 17:00";
NSArray *strings = [dateTime componentsSeparatedByString:#" "];
The array strings will now look like...
[
#"01/02/2013",
#"17:00"
]

If you want to be able to change to time format, or want more control over the times etc. I recommend a NSDateFormatter..
NSString *dateString = #"'01/02/2013 17:00";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/YYYY HH:mm"];
NSDate *date = [formatter dateFromString:dateString];
[formatter setDateFormat:#"dd/MM/YYYY"];
NSString *newDateString = [formatter stringFromDate:date];
[formatter setDateFormat:#"HH:mm"];
NSString *newTimeString = [formatter stringFromDate:date];

Related

Problems with Creating an NSDate object from a ISO 8601 string

I'm having problems converting a string into a NSDate object and I'm not sure why.
NSString* tester= #"2012-11-26T10:20:40.187";
NSLog(#"", tester); //Print the Date String
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *date = [formatter dateFromString:tester]; //Store the date
NSDateFormatter *output = [[NSDateFormatter alloc] init];
[output setDateStyle:NSDateFormatterMediumStyle];
NSLog(#"DATE OBJECT:%#\nDATE STRING: \"%#\"", [output stringFromDate:date]); //Print the NSDate object and the string
But all I seem to get is:
DATE OBJECT:(null)
DATE STRING: "2012-11-26T10:20:40.187"
I figure it has something to do with the .187 but I'm not sure. I'm sorry if this is a duplicate but I couldn't figure this out.
Thank you very much in advance!
The abbrevation for milliseconds is "S"not Z which is TimeZone.
It seems that you have read the correct document where that example is from, but
you missed the link to the Unicode Technical Standard #35.
See NsDateFormatter Docu, search for "Formatters in Mac OS X v10.4 use version tr35-4."
try that below:
[formatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS"];
See also NSDateFormatter Question

Convert Short Zulu format date to NSDate

I am having troubles converting a short Zulu date format to a NSDate Object. I have found some answers for converting Zulu strings but mine looks like:
20111210T1000
And based on my researches, I am trying to do:
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:#"yyyyMMdd'T'HHmmss Z"];
NSDate *date = [f dateFromString:[str stringByReplacingOccurrencesOfString:#"Z" withString:#" +0000"]];
[f release];
I've tried many ways but my date is still nil...
How should I set my NSDateFormatter?
Here is a quick fix, in your date string you miss the timezone in the format, you should append (Paris one here) to your string and it should work. Also the format was wrong.
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"yyyyMMdd'T'SSSZ"];
NSString* str = #"20111210T1000-0100"; // NOTE -0100, GMT +1 Paris zone
NSDate* date = [df dateFromString:str];
NSLog(#"%#", date);

NSDateFormatter in loop

I am having some issues with the following function. I have a dictionary with an array of date strings. I would like to loop through them and generate an NSDate object for each string. An example of the date string would be 20Z01NOV2011, where 20Z indicates 8:00 Zulu time, followed by the day,month, year.To make the date extraction easier, I remove the Z and insert a space.
The date formatter seems to work fine the first loop iteration, however fails on the subsequent iterations, however the input string format seems to be fine. Im not sure if there is a memory issue, and the string or formatter needs to be cleared, but I could use a hand correcting it.
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:#"HH ddMMMyyyy"];
NSMutableArray *tempDates = [[NSMutableArray alloc] initWithCapacity:[[dict objectForKey:#"time"] count]];
NSMutableArray *tempDateStrings = [[NSMutableArray alloc] initWithCapacity:[[dict objectForKey:#"time"] count]];
for (int i=0; i < [[dict objectForKey:#"time"] count]; ++i) {
NSString *dateString = [[[dict objectForKey:#"time"] objectAtIndex:i] stringByReplacingOccurrencesOfString:#"Z" withString:#" "];
NSDate *date = [timeFormatter dateFromString:dateString];
[tempDates addObject:date];
[timeFormatter setDateFormat:#"EEE h:mm a"];
[tempDateStrings addObject:[timeFormatter stringFromDate:date]];
}
[dict setObject:tempDateStrings forKey:#"dateStrings"];
[dict setObject:tempDates forKey:#"dateObjects"];
Side note, I think you should remove the index from the iteration entirely:
Also, you're resetting the formatter inside the loop…
for (NSString *dateString in [dict objectForKey:#"time"]) {
dateString = [dateString stringByReplacingOccurrencesOfString:#"Z" withString:#" "];
NSDate *date = [timeFormatter dateFromString:dateString];
[tempDates addObject:date];
[timeFormatter setDateFormat:#"EEE h:mm a"]; // RESETING DATE FORMAT - SECOND ITERATION WILL FAIL
[tempDateStrings addObject:[timeFormatter stringFromDate:date]];
}
I suspect you want two formatters, ONE to read the string input, and a SECOND to output the value into the format you like.
It fails on subsequent iterations because you MODIFIED THE FORMAT near the bottom of the loop. What you probably want is two separate formatters, one for one format and one for the other, so you don't have to switch formats back and forth.

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

How to set the NSDate on iPhone?

I want to ask 2 questions about the NSDate of iPhone application.
1) How to set the
NSDate *startDay to 01-01-2010 and NSDate *endDay to 31-12-2010
2) how many day between the startDay and the endDay.
What should I do? Thank you very much.
For handling dates with different formats you would need to use NSDateFormatter
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"mm-dd-yyyy"];
To get date with specified format
NSString *dateString = [format stringFromDate:date];
To create a date from string with specified format:
NSDate *date = [format dateFromString:dateString];
NSDateFormatter documentation
To find the difference between two dates:
NSTimeInterval interval = [endDay timeIntervalSinceDate:startDay];
timeIntervalSinceDate documentation