Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 4, 2011, 02:55 PM   #1
johnmerlino
macrumors member
 
Join Date: Oct 2011
formatting date returns (null)

Hey all,

I have a date stored in a postgres database with a time data type. When I send it to objective-c, if I just put it in a field as is, then the field will contain this:

2011-05-10T09:49:00Z

I would like that to be formatted better, so I do this:

Code:
       NSDateFormatter *formatter;
        NSString        *dateString;
        NSDate *date = report.time;
        
        formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"dd-MM-yyyy HH:mm"];
        
        dateString = [formatter stringFromDate:date];
        
        timeTextField.text = [NSString stringWithFormat:@"%@", dateString];
But when I do the above, it converts the date into (null).

thanks for response
johnmerlino is offline   0 Reply With Quote
Old Dec 4, 2011, 04:52 PM   #2
chown33
macrumors 603
 
Join Date: Aug 2009
Apply basic debugging skills.
1. Break It Down.
2. Confirm Expectations.

Break It Down means breaking the problem into sub-problems. The conversion isn't monolithic; it consists of logical sub-parts. Each statement in your original code can have its result confirmed.

Confirm Expectations means testing whether report.time is a) non-nil, and b) of type NSDate.

Example code:
Code:
#import <Foundation/Foundation.h>

int main(int arcgc, char *argv[])
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

	NSDate *date = [NSDate date];  // EXAMPLE ONLY: current date/time

	NSLog( @"date: %@, class: %@", date, NSStringFromClass( [date class] ) ); 

	[NSDateFormatter setDefaultFormatterBehavior: NSDateFormatterBehavior10_4];  // confirm desired behavior will result
	NSDateFormatter * f = [[NSDateFormatter alloc] init];
	[f setDateFormat:@"dd-MM-yyyy HH:mm"];

	NSString * s = [f stringFromDate:date];
	NSLog( @"text: %@", s ); 
	
	[pool drain];
	return 0;
}
Quote:
Originally Posted by johnmerlino View Post
Code:
        timeTextField.text = [NSString stringWithFormat:@"%@", dateString];
The red-hilited code is redundant and unnecessary. dateString is already an immutable string. You don't need another copy of it. Use it as-is.
chown33 is offline   0 Reply With Quote
Old Dec 7, 2011, 02:38 PM   #3
johnmerlino
Thread Starter
macrumors member
 
Join Date: Oct 2011
thanks for response
johnmerlino is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Tags
date format

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
TigerMe Club for iPhone, exclusive dating advisor for men, drops its price from $100 Euforia iPhone and iPod touch Apps 0 Jun 2, 2011 05:33 AM
VIP dating advisor for men: Improve your social life with TigerMe Club for iPhone Euforia iPhone and iPod touch Apps 0 May 12, 2011 05:49 AM
Format date in simple core data program? kellah Mac Programming 2 Apr 7, 2008 11:43 PM


All times are GMT -5. The time now is 12:59 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC