Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 4, 2011, 03: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, 05: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, 03: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

Similar Threads
thread Thread Starter Forum Replies Last Post
Date formatting in Numbers on Mac jootly Mac Applications and Mac App Store 6 Jul 27, 2013 01:28 PM
Formatting Excel Date Error RatHat Mac Applications and Mac App Store 2 Jun 28, 2013 10:44 PM
This drives me nuts - CFBundleCopyResourceURL returns NULL AZAstro iPhone/iPad Programming 8 Sep 29, 2012 03:52 PM
Resolved: self.splitViewController... returns NULL? ArtOfWarfare iPhone/iPad Programming 5 Aug 7, 2012 11:51 PM

Forum Jump

All times are GMT -5. The time now is 08:53 PM.

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

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