NSString with date: reduce size of date - iphone

I have a NSString like Mon Feb 06 20:02:17 +0000 2012. I want to write it in a shorter way, maybe like: DD-MM-YY, HH:MM. I have think that maybe I can convert it to NSDate and again to a shorter NSString, but I don't know how to convert this strange date format to NSDate.
If you need more information, ask me.
Thanks!

You need to use an NSDateFormatter to tell NSDate the format the original date is in and the format you want it in, then you can convert between the two like this:
NSString *oldDate = #"Mon Feb 06 20:02:17 +0000 2012";
NSString *oldFormat = #"EEE MMM dd HH:mm:ss ZZ yyyy";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:oldFormat];
NSDate *date = [dateFormatter dateFromString:sourceString];
NSString *newFormat = #"dd-MM-yy, HH:mm";
[dateFormatter setDateFormat:newFormat];
NSString *newDate = [dateFormatter stringFromDate:date];

There are similar question and answers in StackOverflow and a tutorial that may help you:
Tutorial:
http://iosdevelopertips.com/cocoa/date-formatters-examples-take-3.html
Similar questions:
NSString to NSDate
Convert NSString->NSDate?
How to convert NSDate to NSString?
Convert NSString of a date to an NSDate
EDIT
NSString *dateStr = #"20081122";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMdd"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:date];

Try with NSDateFormatter.
You can convert to NSDate using one of its method:
- (NSDate *)dateFromString:(NSString *)string
And then convert that to whatever you want using NSDateFormatter.
Edit: Actually NSDate has a class method so you may check that as well:
+ (id)dateWithString:(NSString *)aString

Related

NSDateFormatter formatting issue

I am trying to convert NSString into NSDate in 12 hours format. (in iOS 6)
Code :
NSString *Bdt = #"05/23/2012 08:00 AM"
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSDate *bd = [df dateFromString:Bdt];
NSLog(#"%#",bd);
Output:
2012-05-23 08:00:00 +0000 it should be 2012-05-23 08:00 AM
Whats wrong in the code ?
Thanks
If you want date in 2012-05-23 08:00 AM style
Create a dateformatter and setDateFormat as yyyy-MM-dd hh:mm a
NSString *Bdt = #"05/23/2012 08:00 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init] ;
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSDate *bd = [df dateFromString:Bdt];
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm a" ];
NSString *datestr=[dateFormatter stringFromDate:bd];
NSLog(#"%#",datestr);
*Note : I am using ARC, so objects are not released/autoreleased.
EDIT:
NSDate will be in this format ONLY : 2012-05-23 08:00:00 +0000.
For any other format you need to use NSString.
Nothing is wrong with it. You are printing an NSDate in NSLog which is very different than creating an NSString with a specific format.
It seems your confusing the internal NSDate representation with string formatting. NSDate stores the date internally in a way different from how it is represented by humans. Just like NSString stores strings in a format that may not be what you ultimately want it encoded as, eg. ASCII or UTF-8. When you are calling NSLog you are getting a diagnostic message showing the date according to the string returned by - (NSString *)description or possibly - (NSString *)debugDescription.

Parsing custom string gives wrong date

I have a string that has a date format of HH:mm so for example it could 12:00 or 22:00, and I input that into my NSDateFormatter by setting it as the date format. I just need to construct a custom date. The problem is when I have done this and I get my parsed string as 2012-05-17 12:00:00 +0000 if the date is the 17th of May.
NSDate *today = [NSDate date];
NSDateFormatter *output = [[NSDateFormatter alloc] init];
[output setDateFormat:[NSString stringWithFormat:#"yyyy-MM-d %#:00 +0000",#"12:00"]];
NSString *finalTodayString = [output stringFromDate:today];
parsedDateString = [NSString stringWithString:finalTodayString];
The problem is when I parse it again to just include the HH:mm I get something totally different. For example if I have this code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-d HH:mm ZZZ"];
NSDate *date = [dateFormatter dateFromString:fullDateString];
NSDateFormatter *dateFormmater2 = [[NSDateFormatter alloc] init];
[dateFormmater2 setDateFormat:#"HH:mm"];
NSString *string =[dateFormmater2 stringFromDate:date];
Then the string should be 12:00 but instead it becomes 14:00. Please help.
Remove the Greenwich Meridian time "+0000" from the first code

How to make date conversion?

I am getting date in string formate ad "2012-04-11T07:51:10+0000" & I want to convert it to NSDate as "11-04-2012". I dont understand how to use "setDateFormat" for this.
This is what I am doing--
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'+'zzzz"];
Please help.
And whatever NSDate I will get I again want to convert to string ie. only 11-04-2012.
I have searched on ggl. But no satisfactory result :(
please help.
Here is how to parse the string to generate a date:
NSString *dateString = #"2012-04-11T07:51:10+0000";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyy-MM-dd'T'HH:mm:ssZ";
NSDate *date = [formatter dateFromString:dateString];
NSLog(#"Date: %#", date);
Now you can generate a new string representation of the date as follows:
formatter.dateFormat = #"dd-MM-yyyy";
NSString *newDateString = [formatter stringFromDate:date];
NSLog(#"New string: %#", newDateString);
// 11-04-2012
The problem with the dateFormat you were using is that you don't have to escape all the symbols such as 'MM' and that zzzz is not the correct symbol to parse the timezone of type +0000.
Have a look at this documentation for more information.
Try this code.
NSString *dateStr = #"Wed, 22 Jun 2011 12:36:00 +0100"; // change value base on your requirement.
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, d MMM yyyy HH:mm:ss ZZZ"]; // Change format base on dareStr.
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"EEE, MMM d YYYY"]; // Change format base on your output requirement
dateStr = [dateFormat stringFromDate:date];
NSLog(#"Date -- %#",dateStr);
[dateFormat release];

How to get yyyy-mm-dd hh:mm:ss format from 28 Jul 2011 22:33:57 in iPhone?

I have stuck in issue in which i have to convert date format is Thu, 28 Jul 2011 22:33:57 +0000 into yyyy-mm-dd hh:mm:ss
please give me some idea how to do this?
What you want is to use the NSDateFormatter. Something like this:
NSDateFormatter* f = [[[NSDateFormatter alloc] init] autorelease];
[f setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSString* dateString = [f stringFromDate:date];
Do note that the hours will still follow the users selected locale. Use kk:mm:ss to enforce a 24-hour time.
what have you tried? it's difficult to answer questions like this...
first you have to parse the date into an NSDate, use an NSDateFormatter, the incoming format looks like POSIX date format so should be easy.
then you want to output to the format you specify with another NSDateFormatter
You need to use an NSDateformatter to convert the first date to a string with the following syntax.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd hh:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate: [NSDate date]]; //Put whatever date you want to convert
Then if you want the date as an NSDate and you have the string generated above just put the following code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd hh:mm:ss"];
NSDate * date = [dateFormatter dateFromString: dateString]; //String generated above

How I can set a date string from twitter to a NSDATE

The date you get back from twitter is in this format Fri Aug 07 12:40:04 +0000 2009. I am able to assign the value to a NSDate without issue. However, when I attempt to use NSDateFormatter, I get a nil returned to me. What am I missing?
NSDate *createdAt = [messageData objectForKey:#"created_at"];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"M/d/yy HH:mm"];
NSString *dateString = [format stringFromDate:createdAt];
label.text = dateString;
i had the same question, and i could not resolve it with the current above answers. so here is what worked for me:
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
//Wed Dec 01 17:08:03 +0000 2010
[df setDateFormat:#"eee, dd MMM yyyy HH:mm:ss ZZZZ"];
NSDate *date = [df dateFromString:[[tweets objectAtIndex: storyIndex] objectForKey: TWITTER_CREATED_AT_JSON_KEY]];
[df setDateFormat:#"eee MMM dd yyyy"];
NSString *dateStr = [df stringFromDate:date];
where tweets is an NSMutableArray filled with NSDictionary objects, storyIndex being the row int value (in the tableview), and TWITTER_CREATED_AT_JSON_KEY being a constant NSString with value created_at. use the dateStr wherever you wish to display the date
If the object associated with the #"created_at" key is a valid NSDate object, this code should work.
However, I'm guessing that it is actually an NSString. If so, it will produce the behavior you're describing.
If I'm right, the code snippet above is assigning an NSString object to an NSDate reference. NSDictionary returns untyped 'id' objects, so the compiler won't give you a type mismatch warning.
You'll have to use NSDateFormatter to parse the string into an NSDate (see dateFromString:).
It looks likes, Twitter changes the format again and again. These works for me now. Don't forget to set the locale.
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
//Mon, 29 Oct 2012 12:24:50 +0000
[df setDateFormat:#"EEE, dd LLL yyyy HH:mm:ss ZZZ"];
[df setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
NSDate *date = [df dateFromString:[element valueForKey:#"created_at"]];
[df setDateFormat:#"yyyy-MM-dd HH:mm"];
NSString *dateStr = [df stringFromDate:date];
[element valueForKey:#"created_at"] - date from twitter
Above nothing worked for me - try this for Twitter: (I've also included a nice nsdate category for better showing date
lbl = (UILabel *)[cell viewWithTag:16];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
//"created_at": "Tue Jul 09 07:45:01 +0000 2013",
[df setDateFormat:#"EEE MMM d HH:mm:ss ZZZZ yyyy"];
NSTimeInterval timeIntveral = [[df dateFromString:[[_twitterArray objectAtIndex:indexPath.row] objectForKey:#"created_at"]] timeIntervalSince1970];
NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:timeIntveral];
//will work if you added the nsdate category, linked above
NSString *ago = [date timeAgo];
NSLog(#"Output is: \"%#\" %.f", ago,timeIntveral);
lbl.text = ago;
Hope this helps!
First off, what are you using stringFromDate: for? That's if you already have an NSDate and want to make a string representing it.
Moreover, when you do use the date formatter, you're giving it a format string that doesn't match the date string you're trying to interpret.
Change the format string to match your date strings, and use dateFromString: instead of stringFromDate: (with the attendant changes to your variable declarations), and it should work.