-2

I'm using the folowing code to get the date as a string

strStartDate.text=[mOnTimeApp->cDate descriptionWithLocale:nil];

It works but has the hour, minute and second. Is there a way to just get the Month, day and year as a string?

modusCell
  • 13,151
  • 9
  • 53
  • 80
Ted pottel
  • 6,869
  • 21
  • 75
  • 134
  • possible duplicate of [Convert NSDate to NSString](http://stackoverflow.com/questions/576265/convert-nsdate-to-nsstring) – Mike S Aug 11 '14 at 22:59

2 Answers2

0

Passing nil into descriptionWithLocale: is the same as calling description on an instance of NSDate. NSDate Class Reference has the following to say about descriptionWithLocale:

Return Value

A string representation of the receiver, using the given locale, or if the locale argument is nil, in the international format YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM represents the time zone offset in hours and minutes from GMT (for example, “2001-03-24 10:45:32 +0600”)

You can configure an instance of NSDateFormatter to represent the desired format and then use the method stringFromDate: passing in your NSDate object -

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy"];
strStartDate.text = [formatter stringFromDate:mOnTimeApp->cDate];
Bamsworld
  • 5,670
  • 2
  • 32
  • 37
-1

Is this why you are looking for?

+ (NSString*)getAbsoluteDateAndTimeFromString:(NSNumber*)formattedTime
{
    long long ts = [formattedTime longLongValue];
    ts /= 1000;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:ts];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"M/d/yyyy h:mm a"];

    return[dateFormatter stringFromDate:date];
}
modusCell
  • 13,151
  • 9
  • 53
  • 80
hackerinheels
  • 1,141
  • 1
  • 6
  • 14