Method: NSDate#string_with_format
- Defined in:
- lib/sugarcube-nsdate/nsdate.rb
#string_with_format(format) ⇒ Object
Pass in a format string or a Symbol. The Symbol must exist in NSDate::SugarCubeFormats.
See https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1 and http://www.unicode.org/reports/tr35/tr35-19.html#Date_Field_Symbol_Table for more information about date format strings.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/sugarcube-nsdate/nsdate.rb', line 54 def string_with_format(format) if format.is_a?(Symbol) formatters = SugarCubeFormats[format] raise "No format found for #{format.inspect}" unless formatters retval = '' formatters.each do |formatter| case formatter when Symbol retval << string_with_format(formatter.to_s) when String retval << formatter end end return retval else format_template = NSDateFormatter.dateFormatFromTemplate(format, options:0, locale:NSLocale.currentLocale) date_formatter = NSDateFormatter.new date_formatter.setDateFormat(format_template) return date_formatter.stringFromDate(self) end end |