Class: Date
Class Method Summary collapse
Instance Method Summary collapse
- #+(date) ⇒ Object
- #-(date) ⇒ Object
- #<(other) ⇒ Object
- #<<(n) ⇒ Object
- #<=(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
- #>(other) ⇒ Object
- #>=(other) ⇒ Object
- #>>(n) ⇒ Object
- #as_json ⇒ Object
- #clone ⇒ Object
- #day ⇒ Object
- #friday? ⇒ Boolean
-
#initialize(year = undefined, month = undefined, day = undefined) ⇒ Date
constructor
A new instance of Date.
- #monday? ⇒ Boolean
- #month ⇒ Object
- #next ⇒ Object (also: #succ)
- #next_day(n = 1) ⇒ Object
- #next_month ⇒ Object
- #prev_day(n = 1) ⇒ Object
- #prev_month ⇒ Object
- #saturday? ⇒ Boolean
- #strftime(format = '') ⇒ Object
- #sunday? ⇒ Boolean
- #thursday? ⇒ Boolean
- #to_json ⇒ Object
- #to_s ⇒ Object
- #tuesday? ⇒ Boolean
- #wday ⇒ Object
- #wednesday? ⇒ Boolean
- #year ⇒ Object
Constructor Details
#initialize(year = undefined, month = undefined, day = undefined) ⇒ Date
Returns a new instance of Date
21 22 23 |
# File 'opal/stdlib/date.rb', line 21 def initialize(year = undefined, month = undefined, day = undefined) @date = `new Date(year, month - 1, day)` end |
Class Method Details
.parse(string) ⇒ Object
11 12 13 14 |
# File 'opal/stdlib/date.rb', line 11 def parse(string) match = `/^(\d*)-(\d*)-(\d*)/.exec(string)` wrap `new Date(parseInt(match[1]), parseInt(match[2]) - 1, parseInt(match[3]))` end |
.today ⇒ Object
16 17 18 |
# File 'opal/stdlib/date.rb', line 16 def today wrap `new Date()` end |
.wrap(native) ⇒ Object
5 6 7 8 9 |
# File 'opal/stdlib/date.rb', line 5 def wrap(native) instance = allocate `#{instance}.date = #{native}` instance end |
Instance Method Details
#+(date) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'opal/stdlib/date.rb', line 41 def +(date) %x{ if (date.$$is_number) { var result = #{clone}; result.date.setDate(#@date.getDate() + date); return result; } else { #{raise TypeError}; } } end |
#-(date) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'opal/stdlib/date.rb', line 25 def -(date) %x{ if (date.$$is_number) { var result = #{clone}; result.date.setDate(#@date.getDate() - date); return result; } else if (date.date) { return Math.round((#@date - #{date}.date) / (1000 * 60 * 60 * 24)); } else { #{raise TypeError}; } } end |
#<(other) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'opal/stdlib/date.rb', line 54 def <(other) %x{ var a = #@date, b = #{other}.date; a.setHours(0, 0, 0, 0); b.setHours(0, 0, 0, 0); return a < b; } end |
#<<(n) ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'opal/stdlib/date.rb', line 129 def <<(n) %x{ if (!n.$$is_number) { #{raise TypeError}; } return #{self >> `-n`}; } end |
#<=(other) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'opal/stdlib/date.rb', line 63 def <=(other) %x{ var a = #@date, b = #{other}.date; a.setHours(0, 0, 0, 0); b.setHours(0, 0, 0, 0); return a <= b; } end |
#<=>(other) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'opal/stdlib/date.rb', line 90 def <=>(other) %x{ var a = #@date, b = #{other}.date; a.setHours(0, 0, 0, 0); b.setHours(0, 0, 0, 0); if (a < b) { return -1; } else if (a > b) { return 1; } else { return 0; } } end |
#==(other) ⇒ Object Also known as: eql?
108 109 110 111 112 113 |
# File 'opal/stdlib/date.rb', line 108 def ==(other) %x{ var a = #@date, b = other.date; return (a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate()); } end |
#>(other) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'opal/stdlib/date.rb', line 72 def >(other) %x{ var a = #@date, b = #{other}.date; a.setHours(0, 0, 0, 0); b.setHours(0, 0, 0, 0); return a > b; } end |
#>=(other) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'opal/stdlib/date.rb', line 81 def >=(other) %x{ var a = #@date, b = #{other}.date; a.setHours(0, 0, 0, 0); b.setHours(0, 0, 0, 0); return a >= b; } end |
#>>(n) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'opal/stdlib/date.rb', line 115 def >>(n) %x{ if (!n.$$is_number) { #{raise TypeError}; } var result = #{clone}, date = result.date, cur = date.getDate(); date.setDate(1); date.setMonth(date.getMonth() + n); date.setDate(Math.min(cur, days_in_month(date.getFullYear(), date.getMonth()))); return result; } end |
#as_json ⇒ Object
191 192 193 |
# File 'opal/stdlib/json.rb', line 191 def as_json to_s end |
#clone ⇒ Object
141 142 143 |
# File 'opal/stdlib/date.rb', line 141 def clone Date.wrap(`new Date(#@date.getTime())`) end |
#day ⇒ Object
145 146 147 |
# File 'opal/stdlib/date.rb', line 145 def day `#@date.getDate()` end |
#month ⇒ Object
157 158 159 |
# File 'opal/stdlib/date.rb', line 157 def month `#@date.getMonth() + 1` end |
#next ⇒ Object Also known as: succ
161 162 163 |
# File 'opal/stdlib/date.rb', line 161 def next self + 1 end |
#next_day(n = 1) ⇒ Object
165 166 167 |
# File 'opal/stdlib/date.rb', line 165 def next_day(n=1) self + n end |
#next_month ⇒ Object
169 170 171 172 173 174 175 176 177 |
# File 'opal/stdlib/date.rb', line 169 def next_month %x{ var result = #{clone}, date = result.date, cur = date.getDate(); date.setDate(1); date.setMonth(date.getMonth() + 1); date.setDate(Math.min(cur, days_in_month(date.getFullYear(), date.getMonth()))); return result; } end |
#prev_day(n = 1) ⇒ Object
179 180 181 |
# File 'opal/stdlib/date.rb', line 179 def prev_day(n=1) self - n end |
#prev_month ⇒ Object
183 184 185 186 187 188 189 190 191 |
# File 'opal/stdlib/date.rb', line 183 def prev_month %x{ var result = #{clone}, date = result.date, cur = date.getDate(); date.setDate(1); date.setMonth(date.getMonth() - 1); date.setDate(Math.min(cur, days_in_month(date.getFullYear(), date.getMonth()))); return result; } end |
#strftime(format = '') ⇒ Object
197 198 199 200 201 202 203 204 205 |
# File 'opal/stdlib/date.rb', line 197 def strftime(format = '') %x{ if (format == '') { return #{to_s}; } return #@date.$strftime(#{format}); } end |
#to_json ⇒ Object
187 188 189 |
# File 'opal/stdlib/json.rb', line 187 def to_json to_s.to_json end |
#to_s ⇒ Object
217 218 219 220 221 222 223 224 |
# File 'opal/stdlib/date.rb', line 217 def to_s %x{ var d = #@date, year = d.getFullYear(), month = d.getMonth() + 1, day = d.getDate(); if (month < 10) { month = '0' + month; } if (day < 10) { day = '0' + day; } return year + '-' + month + '-' + day; } end |
#wday ⇒ Object
230 231 232 |
# File 'opal/stdlib/date.rb', line 230 def wday `#@date.getDay()` end |
#wednesday? ⇒ Boolean
234 235 236 |
# File 'opal/stdlib/date.rb', line 234 def wednesday? wday == 3 end |
#year ⇒ Object
238 239 240 |
# File 'opal/stdlib/date.rb', line 238 def year `#@date.getFullYear()` end |