Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here's my server time:

2014-08-20 16:23:00.0

Now I would like to format the date my server gives me with the date only, and not the time. For example: Month/date/year Could someone explain what I am doing wrong?

Here's what I have tried:

 NSDate * date = [[NSDate alloc] init];

    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];

    [formatter setDateFormat:@"yyyy-dd-MM HH:mm:ss"];

   NSString * dateForShow = //2014-08-20 16:23:00.0
    //Date returns nil
    date = [formatter dateFromString:dateForShow];

    [formatter setDateFormat:@"MM-dd-yyyy"];


    NSString * result = [formatter stringFromDate:date];
share|improve this question
4  
Look at the actual date you have. Look at the date format string. How do they differ?? Then look at unicode.org/reports/tr35/tr35-31/…;. How should the date format string be modified to look more like the actual date? –  Hot Licks 2 hours ago
    
Better link to the ICU date formatting document: ICU Formatting Dates and Times –  Zaph 1 hour ago

3 Answers 3

up vote 0 down vote accepted

Try this for your third line:

[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.S"];

Reference: Link

share|improve this answer
    
Why didn't you reference the spec instead? –  Hot Licks 1 hour ago
    
I still get a nil response. –  wayon 1 hour ago
    
wayon, please see my edited answer. We both had month and day mixed up. @HotLicks, what do you mean? –  rebello95 1 hour ago
    
Rather than point at another SO issue, point at the spec, which every serious app programmer should have bookmarked (applies to Android as well). –  Hot Licks 1 hour ago
    
You're right, I've updated my answer. Funny thing is I had that page bookmarked but for some reason didn't think to reference it. Thanks for the correction. –  rebello95 1 hour ago

Use this format @"yyyy-MM-dd HH:mm:ss.S"

share|improve this answer

First, if you are getting date from String, you have the same date format as your string format.

See my examples and better read the Date and Time Specs from Apple:

NSString *beginString = [currentFields objectForKey:@"pubDate"]; 

Here I am fetching date and time in following format: Thu, 31 Jul 2014 16:01:09 +0300

NSDateformatter format init

        dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"]];
        [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Kiev"]];
        [dateFormat setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"];
        dateFromString = [dateFormat dateFromString:beginString];

        //NSLog(@"Begin string: %@", beginString);

Convert Date to string

        [dateFormat setTimeZone:[NSTimeZone localTimeZone]];
        [dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"ru_RU"]];
        [dateFormat setDateFormat:@"dd MMMM yyyy"];
        myStrDate = [dateFormat stringFromDate:dateFromString];
        [currentTitle setPubDate:myStrDate];

After converting in formatting my date will be:

31 July 2014

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.