Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-09-2012, 03:28 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2012
Location: Hyderabad, Andhra Pradesh, INDIA
Posts: 3
pavanindira is on a distinguished road
Default Trying to convert dateFromString. please help

Hi Friends,

I am trying to sort date by using the below code, but this dose not work, can anyone please help me.

NSString *str = [NSString stringWithFormat:@"%@",[NSDate date]];
NSDateFormatter *coll = [[[NSDateFormatter alloc] init]autorelease];
NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
[coll setLocale:locale];
[coll setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"];
//[form setDateStyle:NSDateFormatterMediumStyle];
//[form setTimeStyle:NSDateFormatterMediumStyle];
//[form setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date = nil;
date =[coll dateFromString:str];
NSLog(@"date has been tested %@",date);

Thanks in Advance.
Pavan CH
pavanindira is offline   Reply With Quote
Old 02-09-2012, 08:51 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by pavanindira View Post
Hi Friends,

I am trying to sort date by using the below code, but this dose not work, can anyone please help me.

NSString *str = [NSString stringWithFormat:@"%@",[NSDate date]];
NSDateFormatter *coll = [[[NSDateFormatter alloc] init]autorelease];
NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
[coll setLocale:locale];
[coll setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"];
//[form setDateStyle:NSDateFormatterMediumStyle];
//[form setTimeStyle:NSDateFormatterMediumStyle];
//[form setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date = nil;
date =[coll dateFromString:str];
NSLog(@"date has been tested %@",date);

Thanks in Advance.
Pavan CH
You said "this dose not work". That is really, really NOT helpful.

What does it do? What date strings are you testing it with, and what are you getting as a result? Be specific, man!
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-09-2012, 10:14 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2012
Location: Hyderabad, Andhra Pradesh, INDIA
Posts: 3
pavanindira is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
You said "this dose not work". That is really, really NOT helpful.

What does it do? What date strings are you testing it with, and what are you getting as a result? Be specific, man!
Sorry for not providing details about the requirement in detail.

I am tring to get date from the system date using "NSDate" and this should be sorted(most recent first) and displayed as "MMM dd yyyy" (Ex: Dec 25 2012).

So i am tring to get the date and assigning it to a NSDate variable, using the below code. But at the end i am left with a NIL value in the log.
pavanindira is offline   Reply With Quote
Old 02-09-2012, 11:06 PM   #4 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 94
JamesCahall is on a distinguished road
Default

Quote:
Originally Posted by pavanindira View Post
Sorry for not providing details about the requirement in detail.

I am tring to get date from the system date using "NSDate" and this should be sorted(most recent first) and displayed as "MMM dd yyyy" (Ex: Dec 25 2012).

So i am tring to get the date and assigning it to a NSDate variable, using the below code. But at the end i am left with a NIL value in the log.
You are getting nil because you are trying to convert a string that is not in the format that you specified in the date formatter and converting it to an NSDate.
It has no idea how to convert your string.

NSLog(@"str: %@", str);
"str: 2012-02-10 05:01:33 +0000"

This works:
NSString *dateString = [coll stringFromDate:[NSDate date]];
NSLog(@"Date String: %@", dateString);
"Date String: Thu, 09 Feb 2012 21:05:31 PST"
NSDate *date = [coll dateFromString:dateString];
NSLog(@"date has been tested %@",date);
"date has been tested 2012-02-10 05:05:31 +0000"
JamesCahall is offline   Reply With Quote
Old 02-10-2012, 12:14 AM   #5 (permalink)
Registered Member
 
Join Date: Feb 2012
Location: Hyderabad, Andhra Pradesh, INDIA
Posts: 3
pavanindira is on a distinguished road
Default

Quote:
Originally Posted by JamesCahall View Post
You are getting nil because you are trying to convert a string that is not in the format that you specified in the date formatter and converting it to an NSDate.
It has no idea how to convert your string.

NSLog(@"str: %@", str);
"str: 2012-02-10 05:01:33 +0000"

This works:
NSString *dateString = [coll stringFromDate:[NSDate date]];
NSLog(@"Date String: %@", dateString);
"Date String: Thu, 09 Feb 2012 21:05:31 PST"
NSDate *date = [coll dateFromString:dateString];
NSLog(@"date has been tested %@",date);
"date has been tested 2012-02-10 05:05:31 +0000"
Hi James,

yes what you specified is working, but my requirement is to get the date in "dd MMM yyyy" format.

thanks for the help.
I am still not able to get the desired output.
pavanindira is offline   Reply With Quote
Old 02-10-2012, 12:18 AM   #6 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 94
JamesCahall is on a distinguished road
Default

Quote:
Originally Posted by pavanindira View Post
Hi James,

yes what you specified is working, but my requirement is to get the date in "dd MMM yyyy" format.

thanks for the help.
I am still not able to get the desired output.
Your output and usable string IS in day month year format:

See line:
NSLog(@"Date String: %@", dateString);

NSDate does not have a format in itself. NSDateFormatter simply translates dates to strings and visa versa based on a format and other optional variables such as locale.

James
JamesCahall is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 402
13 members and 389 guests
chiataytuday, chits12345, fiftysixty, gmarro, iOS.Lover, KennyChong, kilobytedump, Leslie80, Matrix23, Meoz, ryantcb, xerohuang, Yosh_K
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,670
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Yosh_K
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:24 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0