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

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.

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.

change date format in iPhone

I am trying this since last two hours and finaly left it out for you guys :P
I need to convert this String Mon, 14 May 2012, 12:00:55 +0200
into Date dd/mm/yyyy hh:ss format..
Any kind of help towards the goal will be really appreciated.
What I have tried
I have tried using NSDateFormatter but I am unable to figure out the exact format of the above date.
This [dateFormatter setDateFormat:#"EEEddMM,yyyy HH:mm:ss"]; is how I have tried and many other formats too
Eg:
NSString *finalDate = #"Tue, 29 May 2012, 14:24:56 +0200";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEEddMM,yyyy HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:finalDate];
Here the date alwasys comes as nil
The most important part of date formatting is often forgotten, tell the NSDateFormatter the input language:
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"EEE, dd MMMM yyyy, HH:mm:ss Z"];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"EN"];
NSDate *date = [dateFormatter dateFromString:#"Mon, 14 May 2012, 12:00:55 +0200"];
NSLog(#"date: %#", date);
I've checked the output: date: 2012-05-14 10:00:55 +0000
Be aware that the HH in the date formatter is for 24hr.
Now I would suggest not to use a fixed output scheme, but use one of the NSDateFormatterStyle:
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"date: %#", dateString);
Which in american english will output: 5/14/12 12:00 PM
This code is valid for ARC if you are not using ARC add autorelease to the NSLocale and release the NSDateFormatter after you are done with it.
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"dd/mm/yyyy hh:ss"];
NSString *formatterDate = [inputFormatter stringFromDate:inDate];
[inputFormatter release];
get inforamtion about all Date Formate
my Blog Link is http://parasjoshi3.blogspot.in/2012/01/date-formate-info-for-iphone-sdk.html
and also small function is bellow.....
-(NSString *) dateInFormat:(NSString*) stringFormat {
char buffer[80];
const char *format = [stringFormat UTF8String];
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer, 80, format, timeinfo);
return [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
}
////////Like.......
NSString *mydate = [self dateInFormat:#"%Y%m%d-%H%M%S"];
hope,this help you....

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

NSString with date: reduce size of date

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

Problem in setting nsdate value from a string

NSDate *My_StartDate,*My_EndDate ;
NSDateFormatter * df= [[NSDateFormatter alloc]init];
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
My_StartDate = [df dateFromString:#"01/05/2010 10:15:33"];
My_EndDate = [df dateFromString:#"01/05/2010 10:45:33"];
NSLog(#"%#",My_StartDate);
NSLog(#"%#",My_EndDate);
In the log i get something like this for the my_startdate as 2010-05-01 04:45:33 +0000 and end date as 2010-05-01 05:15:33 +0000 instead i should have got value as for start date as 2010-05-01 10:15:33 +0000 and end date as 2010-05-01 10:45:33 +0000
Try with below function:
-(NSString *)getDateStringFromDate :(NSDate *)dateValue{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setTimeStyle:NSDateFormatterShortStyle];
[timeFormat setDateFormat:#"HH:mm:ss"];
//[timeFormat setDateFormat:#"HH:mm"];
//[timeFormat setDateFormat:#"HH:mm a"];
////
NSString *theDate = [dateFormat stringFromDate:dateValue];
NSString *theTime = [timeFormat stringFromDate:dateValue];
NSLog(#"\n"
"theDate: |%#| \n"
"theTime: |%#| \n"
, theDate, theTime);
return theDate;
}
Change Format of data as per your need.
Let me know in case of any difficulty.
Cheers.
This shows date which follow American standard time string but by this reason you don't get any problem in making your logic.Also
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
this format using 12 hour format (means 2:03 pm and 2:03 am) and date object never use am and pm for showing date object value but when you convert it correctly then it gives you right date and time.
If you feel you get any problem then use different locale for that.
It is displaying asper the GMT+4.30 time.It displays like that only.When you are converting that date to string using the DateFormatter it gives the same date(Whichever you want like start date as 01/05/2010 10:15:33 and end date as 01/05/2010 10:45:33).
NSDateFormatter * dateformatter= [[NSDateFormatter alloc]init];
[dateformatter setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
NSString *dat = [dateformatter stringfromDate:My_StartDate];
then you will get the output as 01/05/2010 10:15:33
You might want to set the time zone of the date formatter to GMT here. Do it using
[df setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
before you do dateFromString: calls. This will give you what you want.
Just need to update here in your code:
I might be like that your time would be in 24 hours format, so at that time you need to use this ....other than that you need to set the timezone.
Follow this link for All zone : http://unicode.org/reports/tr35/tr35-6.html#Date%5FFormat%5FPatterns
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
to
[df setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
You are Done ;)