Class: NSDateFormatter

Inherits:
NSFormatter show all

Overview

Instances of NSDateFormatter create string representations of NSDate objects, and convert textual representations of dates and times into NSDate objects. You can express the representation of dates and times flexibly using pre-set format styles or custom format strings.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from NSFormatter

#attributedStringForObjectValue:withDefaultAttributes:, #editingStringForObjectValue:, #getObjectValue:forString:errorDescription:, #isPartialStringValid:newEditingString:errorDescription:, #isPartialStringValid:proposedSelectedRange:originalString:originalSelectedRange:errorDescription:, #stringForObjectValue:

Methods inherited from NSObject

#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version

Constructor Details

This class inherits a constructor from NSObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class NSObject

Class Method Details

+ (String) dateFormatFromTemplate(template, options:opts, locale:locale)

Returns a localized date format string representing the given date format components arranged appropriately for the specified locale. Different locales have different conventions for the ordering of date components. You use this method to get an appropriate format string for a given set of components for a specified locale (typically you use the current locale—see currentLocale). The following example shows the difference between the date formats for British and American English:

Parameters:

  • template (String)

    A string containing date format patterns (such as “MM” or “h”).For full details, see Date and Time Programming Guide.

  • opts (Integer)

    No options are currently defined—pass 0.

  • locale (NSLocale)

    The locale for which the template is required.

Returns:

  • (String)

    A localized date format string representing the date format components given in template, arranged appropriately for the locale specified by locale.The returned string may not contain exactly those components given in template, but may—for example—have locale-specific adjustments applied.

+ (NSDateFormatterBehavior) defaultFormatterBehavior

Returns the default formatting behavior for instances of the class. For iOS and for OS X applications linked against OS X v10.5 and later, the default is NSDateFormatterBehavior10_4.

Returns:

+ (String) localizedStringFromDate(date, dateStyle:dateStyle, timeStyle:timeStyle)

Returns string representation of a given date formatted for the current locale using the specified date and time styles. This method uses a date formatter configured with the current default settings. The returned string is the same as if you configured and used a date formatter as shown in the following example:

Parameters:

Returns:

  • (String)

    A localized string representation of date using the specified date and time styles

+ (Object) setDefaultFormatterBehavior(behavior)

Sets the default formatting behavior for instances of the class.

Parameters:

Returns:

Instance Method Details

- (String) AMSymbol

Returns the AM symbol for the receiver.

Returns:

  • (String)

    The AM symbol for the receiver.

- (NSCalendar) calendar

Returns the calendar for the receiver.

Returns:

- (String) dateFormat

Returns the date format string used by the receiver. See Data Formatting Guide for a list of the conversion specifiers permitted in date format strings.

Returns:

  • (String)

    The date format string used by the receiver.

- (NSDate) dateFromString(string)

Returns a date representation of a given string interpreted using the receiver’s current settings.

Parameters:

  • string (String)

    The string to parse.

Returns:

  • (NSDate)

    A date representation of string interpreted using the receiver’s current settings.

- (NSDateFormatterStyle) dateStyle

Returns the date style of the receiver.

Returns:

- (NSDate) defaultDate

Returns the default date for the receiver. The default default date is nil.

Returns:

  • (NSDate)

    The default date for the receiver.

- (Boolean) doesRelativeDateFormatting

Returns a Boolean value that indicates whether the receiver uses phrases such as “today” and “tomorrow” for the date component. For a full discussion, see setDoesRelativeDateFormatting:.

Returns:

  • (Boolean)

    YES if the receiver uses relative date formatting, otherwise NO.

- (Array) eraSymbols

Returns the era symbols for the receiver.

Returns:

  • (Array)

    An array containing NSString objects representing the era symbols for the receiver (for example, “C.E.”).

- (NSDateFormatterBehavior) formatterBehavior

Returns the formatter behavior for the receiver.

Returns:

- (Boolean) generatesCalendarDates

You should not use this method. NSCalendarDate is no longer supported; you should not use this method.

Returns:

  • (Boolean)

    YES if the receiver generates calendar dates, otherwise NO.

- (Boolean) getObjectValue(obj, forString:string, range:rangep, error:error)

Returns by reference a date representation of a given string and the range of the string used, and returns a Boolean value that indicates whether the string could be parsed.

Parameters:

  • obj (Object)

    If the receiver is able to parse string, upon return contains a date representation of string.

  • string (String)

    The string to parse.

  • rangep (inout NSRange)

    If the receiver is able to parse string, upon return contains the range of string used to create the date.

  • error (Pointer)

    If the receiver is unable to create a date by parsing string, upon return contains an NSError object that describes the problem.

Returns:

  • (Boolean)

    YES if the receiver can create a date by parsing string, otherwise NO.

- (NSDate) gregorianStartDate

Returns the start date of the Gregorian calendar for the receiver.

Returns:

  • (NSDate)

    The start date of the Gregorian calendar for the receiver.

- (Boolean) isLenient

Returns a Boolean value that indicates whether the receiver uses heuristics when parsing a string.

Returns:

  • (Boolean)

    YES if the receiver has been set to use heuristics when parsing a string to guess at the date which is intended, otherwise NO.

- (NSLocale) locale

Returns the locale for the receiver.

Returns:

  • (NSLocale)

    The locale for the receiver.

- (Array) longEraSymbols

Returns the long era symbols for the receiver

Returns:

  • (Array)

    An array containing NSString objects representing the era symbols for the receiver (for example, Common Era”, “Common Era”).

- (Array) monthSymbols

Returns the month symbols for the receiver.

Returns:

  • (Array)

    An array of NSString objects that specify the month symbols for the receiver.

- (String) PMSymbol

Returns the PM symbol for the receiver.

Returns:

  • (String)

    The PM symbol for the receiver.

- (Array) quarterSymbols

Returns the quarter symbols for the receiver.

Returns:

  • (Array)

    An array containing NSString objects representing the quarter symbols for the receiver.

- (Object) setAMSymbol(string)

Sets the AM symbol for the receiver.

Parameters:

  • string (String)

    The AM symbol for the receiver.

Returns:

- (Object) setCalendar(calendar)

Sets the calendar for the receiver.

Parameters:

  • calendar (NSCalendar)

    The calendar for the receiver.

Returns:

- (Object) setDateFormat(string)

Sets the date format for the receiver.

Parameters:

  • string (String)

    The date format for the receiver. See Data Formatting Guide for a list of the conversion specifiers permitted in date format strings.

Returns:

- (Object) setDateStyle(style)

Sets the date style of the receiver.

Parameters:

Returns:

- (Object) setDefaultDate(date)

Sets the default date for the receiver.

Parameters:

  • date (NSDate)

    The default date for the receiver.

Returns:

- (Object) setDoesRelativeDateFormatting(b)

Specifies whether the receiver uses phrases such as “today” and “tomorrow” for the date component. If a date formatter uses relative date formatting, where possible it replaces the date component of its output with a phrase—such as “today” or “tomorrow”—that indicates a relative date. The available phrases depend on the locale for the date formatter; whereas, for dates in the future, English may only allow “tomorrow,” French may allow “the day after the day after tomorrow,” as illustrated in the following example.

Parameters:

  • b (Boolean)

    YES to specify that the receiver should use relative date formatting, otherwise NO.

Returns:

- (Object) setEraSymbols(array)

Sets the era symbols for the receiver.

Parameters:

  • array (Array)

    An array containing NSString objects representing the era symbols for the receiver (for example, “C.E.”).

Returns:

- (Object) setFormatterBehavior(behavior)

Sets the formatter behavior for the receiver.

Parameters:

Returns:

- (Object) setGeneratesCalendarDates(b)

You should not use this method. NSCalendarDate is no longer supported; you should not use this method.

Parameters:

  • b (Boolean)

    A Boolean value that specifies whether the receiver generates calendar dates.

Returns:

- (Object) setGregorianStartDate(date)

Sets the start date of the Gregorian calendar for the receiver.

Parameters:

  • date (NSDate)

    The start date of the Gregorian calendar for the receiver.

Returns:

- (Object) setLenient(b)

Sets whether the receiver uses heuristics when parsing a string. If a formatter is set to be lenient, when parsing a string it uses heuristics to guess at the date which is intended. As with any guessing, it may get the result date wrong (that is, a date other than that which was intended).

Parameters:

  • b (Boolean)

    YES to use heuristics when parsing a string to guess at the date which is intended, otherwise NO.

Returns:

- (Object) setLocale(locale)

Sets the locale for the receiver.

Parameters:

  • locale (NSLocale)

    The locale for the receiver.

Returns:

- (Object) setLongEraSymbols(array)

Sets the long era symbols for the receiver.

Parameters:

  • array (Array)

    An array containing NSString objects representing the era symbols for the receiver (for example, Common Era”, “Common Era”).

Returns:

- (Object) setMonthSymbols(array)

Sets the month symbols for the receiver.

Parameters:

  • array (Array)

    An array of NSString objects that specify the month symbols for the receiver.

Returns:

- (Object) setPMSymbol(string)

Sets the PM symbol for the receiver.

Parameters:

  • string (String)

    The PM symbol for the receiver.

Returns:

- (Object) setQuarterSymbols(array)

Sets the quarter symbols for the receiver.

Parameters:

  • array (Array)

    An array of NSString objects that specify the quarter symbols for the receiver.

Returns:

- (Object) setShortMonthSymbols(array)

Sets the short month symbols for the receiver.

Parameters:

  • array (Array)

    An array of NSString objects that specify the short month symbols for the receiver.

Returns:

- (Object) setShortQuarterSymbols(array)

Sets the short quarter symbols for the receiver.

Parameters:

  • array (Array)

    An array of NSString objects that specify the short quarter symbols for the receiver.

Returns:

- (Object) setShortStandaloneMonthSymbols(array)

Sets the short standalone month symbols for the receiver.

Parameters:

  • array (Array)

    An array of NSString objects that specify the short standalone month symbols for the receiver.

Returns:

- (Object) setShortStandaloneQuarterSymbols(array)

Sets the short standalone quarter symbols for the receiver.

Parameters:

  • array (Array)

    An array of NSString objects that specify the short standalone quarter symbols for the receiver.

Returns:

- (Object) setShortStandaloneWeekdaySymbols(array)

Sets the short standalone weekday symbols for the receiver.

Parameters:

  • array (Array)

    An array of NSString objects that specify the short standalone weekday symbols for the receiver.

Returns:

- (Object) setShortWeekdaySymbols(array)

Sets the short weekday symbols for the receiver.

Parameters:

  • array (Array)

    An array of NSString objects that specify the short weekday symbols for the receiver.

Returns:

- (Object) setStandaloneMonthSymbols(array)

Sets the standalone month symbols for the receiver.

Parameters:

  • array (Array)

    An array of NSString objects that specify the standalone month symbols for the receiver.

Returns:

- (Object) setStandaloneQuarterSymbols(array)

Sets the standalone quarter symbols for the receiver.

Parameters:

  • array (Array)

    An array of NSString objects that specify the standalone quarter symbols for the receiver.

Returns:

- (Object) setStandaloneWeekdaySymbols(array)

Sets the standalone weekday symbols for the receiver.

Parameters:

  • array (Array)

    An array of NSString objects that specify the standalone weekday symbols for the receiver.

Returns:

- (Object) setTimeStyle(style)

Sets the time style of the receiver.

Parameters:

Returns:

- (Object) setTimeZone(tz)

Sets the time zone for the receiver.

Parameters:

  • tz (NSTimeZone)

    The time zone for the receiver.

Returns:

- (Object) setTwoDigitStartDate(date)

Sets the two-digit start date for the receiver.

Parameters:

  • date (NSDate)

    The earliest date that can be denoted by a two-digit year specifier.

Returns:

- (Object) setVeryShortMonthSymbols(array)

Sets the very short month symbols for the receiver.

Parameters:

  • array (Array)

    An array of NSString objects that specify the very short month symbols for the receiver.

Returns:

- (Object) setVeryShortStandaloneMonthSymbols(array)

Sets the very short standalone month symbols for the receiver.

Parameters:

  • array (Array)

    An array of NSString objects that specify the very short standalone month symbols for the receiver.

Returns:

- (Object) setVeryShortStandaloneWeekdaySymbols(array)

Sets the very short standalone weekday symbols for the receiver.

Parameters:

  • array (Array)

    An array of NSString objects that specify the very short standalone weekday symbols for the receiver.

Returns:

- (Object) setVeryShortWeekdaySymbols(array)

Sets the vert short weekday symbols for the receiver

Parameters:

  • array (Array)

    An array of NSString objects that specify the very short weekday symbols for the receiver.

Returns:

- (Object) setWeekdaySymbols(array)

Sets the weekday symbols for the receiver.

Parameters:

  • array (Array)

    An array of NSString objects that specify the weekday symbols for the receiver.

Returns:

- (Array) shortMonthSymbols

Returns the array of short month symbols for the receiver.

Returns:

  • (Array)

    An array containing NSString objects representing the short month symbols for the receiver.

- (Array) shortQuarterSymbols

Returns the short quarter symbols for the receiver.

Returns:

  • (Array)

    An array containing NSString objects representing the short quarter symbols for the receiver.

- (Array) shortStandaloneMonthSymbols

Returns the short standalone month symbols for the receiver.

Returns:

  • (Array)

    An array of NSString objects that specify the short standalone month symbols for the receiver.

- (Array) shortStandaloneQuarterSymbols

Returns the short standalone quarter symbols for the receiver.

Returns:

  • (Array)

    An array containing NSString objects representing the short standalone quarter symbols for the receiver.

- (Array) shortStandaloneWeekdaySymbols

Returns the array of short standalone weekday symbols for the receiver.

Returns:

  • (Array)

    An array of NSString objects that specify the short standalone weekday symbols for the receiver.

- (Array) shortWeekdaySymbols

Returns the array of short weekday symbols for the receiver.

Returns:

  • (Array)

    An array of NSString objects that specify the short weekday symbols for the receiver.

- (Array) standaloneMonthSymbols

Returns the standalone month symbols for the receiver.

Returns:

  • (Array)

    An array of NSString objects that specify the standalone month symbols for the receiver.

- (Array) standaloneQuarterSymbols

Returns the standalone quarter symbols for the receiver.

Returns:

  • (Array)

    An array containing NSString objects representing the standalone quarter symbols for the receiver.

- (Array) standaloneWeekdaySymbols

Returns the array of standalone weekday symbols for the receiver.

Returns:

  • (Array)

    An array of NSString objects that specify the standalone weekday symbols for the receiver.

- (String) stringFromDate(date)

Returns a string representation of a given date formatted using the receiver’s current settings.

Parameters:

  • date (NSDate)

    The date to format.

Returns:

  • (String)

    A string representation of date formatted using the receiver’s current settings.

- (NSDateFormatterStyle) timeStyle

Returns the time style of the receiver.

Returns:

- (NSTimeZone) timeZone

Returns the time zone for the receiver.

Returns:

  • (NSTimeZone)

    The time zone for the receiver.

- (NSDate) twoDigitStartDate

Returns the earliest date that can be denoted by a two-digit year specifier. If the two-digit start date is set to January 6, 1976, then “January 1, 76” is interpreted as New Year’s Day in 2076, whereas “February 14, 76” is interpreted as Valentine’s Day in 1976.The default date is December 31, 1949.

Returns:

  • (NSDate)

    The earliest date that can be denoted by a two-digit year specifier.

- (Array) veryShortMonthSymbols

Returns the very short month symbols for the receiver.

Returns:

  • (Array)

    An array of NSString objects that specify the very short month symbols for the receiver.

- (Array) veryShortStandaloneMonthSymbols

Returns the very short month symbols for the receiver.

Returns:

  • (Array)

    An array of NSString objects that specify the very short standalone month symbols for the receiver.

- (Array) veryShortStandaloneWeekdaySymbols

Returns the array of very short standalone weekday symbols for the receiver.

Returns:

  • (Array)

    An array of NSString objects that specify the very short standalone weekday symbols for the receiver.

- (Array) veryShortWeekdaySymbols

Returns the array of very short weekday symbols for the receiver.

Returns:

  • (Array)

    An array of NSString objects that specify the very short weekday symbols for the receiver.

- (Array) weekdaySymbols

Returns the array of weekday symbols for the receiver.

Returns:

  • (Array)

    An array of NSString objects that specify the weekday symbols for the receiver.