Вопрос

I have the following NSString:

NSString * dateString = @"2012-01-24T14:59:01Z";

I want to create an NSDate from that string. I looked up the NSDate Class Reference and thought of using dateWithNaturalLanguageString::

Creates and returns an NSDate object set to the date and time specified by a given string.

+ (id)dateWithNaturalLanguageString:(NSString *)string

Parameters string A string that contains a colloquial specification of a date, such as “last Tuesday at dinner,” “3pm December 31, 2001,” “12/31/01,” or “31/12/01.” Return Value A new NSDate object set to the current date and time specified by string.

However, when I'm trying to use it like this:

NSDate * date = [NSDate dateWithNaturalLanguageString:dateString];

I'm getting the following error:

No known class method for selector 'dateWithNaturalLanguageString:'

Это было полезно?

Решение 4

Found exactly what I was looking for here. It's an RFC 3339 date-time.

- (NSString *)userVisibleDateTimeStringForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString
    // Returns a user-visible date time string that corresponds to the
    // specified RFC 3339 date time string. Note that this does not handle
    // all possible RFC 3339 date time strings, just one of the most common
    // styles.
{
    NSString *          userVisibleDateTimeString;
    NSDateFormatter *   rfc3339DateFormatter;
    NSLocale *          enUSPOSIXLocale;
    NSDate *            date;
    NSDateFormatter *   userVisibleDateFormatter;

    userVisibleDateTimeString = nil;

    // Convert the RFC 3339 date time string to an NSDate.

    rfc3339DateFormatter = [[[NSDateFormatter alloc] init] autorelease];

    enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];

    [rfc3339DateFormatter setLocale:enUSPOSIXLocale];
    [rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
    [rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    date = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];
    if (date != nil) {

        // Convert the NSDate to a user-visible date string.

        userVisibleDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
        assert(userVisibleDateFormatter != nil);

        [userVisibleDateFormatter setDateStyle:NSDateFormatterShortStyle];
        [userVisibleDateFormatter setTimeStyle:NSDateFormatterShortStyle];

        userVisibleDateTimeString = [userVisibleDateFormatter stringFromDate:date];
    }
    return userVisibleDateTimeString;
}

Другие советы

NSDateFormatter class will help you with this problem. And there are many questions about this already, for example, here is the first one: Convert NSString->NSDate?

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

Try using the constructor dateWithString constructor or (if that gives you a similar error), try using an NSDateFormatter as described here.

The dateWithNaturalLanguageString: class method is only implemented on Mac OS X, not on iOS, that is why you're getting the error.

To achieve what you're looking for, you need the NSDateFormatter class. The class is quite heavy, so you'll want to read the documentation first to understand how best to use it.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top