Method: NSDate#string_with_format

Defined in:
lib/sugarcube-nsdate/nsdate.rb

#string_with_format(format) ⇒ Object



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