Class: Date
Class Method Summary collapse
Instance Method Summary collapse
- #-(date) ⇒ Object
- #<(other) ⇒ Object
- #<=(other) ⇒ Object
- #==(other) ⇒ Object
- #>(other) ⇒ Object
- #>=(other) ⇒ Object
- #as_json ⇒ Object
- #clone ⇒ Object
- #day ⇒ Object
-
#initialize(year = undefined, month = undefined, day = undefined) ⇒ Date
constructor
A new instance of Date.
- #month ⇒ Object
- #next ⇒ Object
- #next_month ⇒ Object
- #prev ⇒ Object
- #prev_month ⇒ Object
- #strftime(format = '') ⇒ Object
- #to_json ⇒ Object
- #to_s ⇒ Object
- #wday ⇒ Object
- #year ⇒ Object
Constructor Details
#initialize(year = undefined, month = undefined, day = undefined) ⇒ Date
Returns a new instance of Date
22 23 24 |
# File 'opal/stdlib/date.rb', line 22 def initialize(year = undefined, month = undefined, day = undefined) `#{self}._date = new native_date(year, month - 1, day)` end |
Class Method Details
.parse(string) ⇒ Object
10 11 12 |
# File 'opal/stdlib/date.rb', line 10 def self.parse(string) wrap `native_date.parse(string)` end |
.today ⇒ Object
14 15 16 17 18 19 20 |
# File 'opal/stdlib/date.rb', line 14 def self.today %x{ var date = #{new}; date._date = new native_date(); return date; } end |
.wrap(native) ⇒ Object
4 5 6 7 8 |
# File 'opal/stdlib/date.rb', line 4 def self.wrap(native) instance = allocate `#{instance}._date = #{native}` instance end |
Instance Method Details
#-(date) ⇒ Object
26 27 28 |
# File 'opal/stdlib/date.rb', line 26 def -(date) `Math.round((#{self}._date - #{date}._date) / (1000 * 60 * 60 * 24))` end |
#<(other) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'opal/stdlib/date.rb', line 30 def <(other) %x{ var a = #{self}._date, b = #{other}._date; a.setHours(0, 0, 0, 0); b.setHours(0, 0, 0, 0); return a < b; } end |
#<=(other) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'opal/stdlib/date.rb', line 39 def <=(other) %x{ var a = #{self}._date, b = #{other}._date; a.setHours(0, 0, 0, 0); b.setHours(0, 0, 0, 0); return a <= b; } end |
#==(other) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'opal/stdlib/date.rb', line 66 def ==(other) %x{ var a = #{self}._date, b = #{other}._date; a.setHours(0, 0, 0, 0); b.setHours(0, 0, 0, 0); return (a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate()); } end |
#>(other) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'opal/stdlib/date.rb', line 48 def >(other) %x{ var a = #{self}._date, b = #{other}._date; a.setHours(0, 0, 0, 0); b.setHours(0, 0, 0, 0); return a > b; } end |
#>=(other) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'opal/stdlib/date.rb', line 57 def >=(other) %x{ var a = #{self}._date, b = #{other}._date; a.setHours(0, 0, 0, 0); b.setHours(0, 0, 0, 0); return a >= b; } end |
#as_json ⇒ Object
126 127 128 |
# File 'opal/stdlib/date.rb', line 126 def as_json to_s end |
#clone ⇒ Object
75 76 77 |
# File 'opal/stdlib/date.rb', line 75 def clone Date.wrap(`new native_date(#{self}._date.getTime())`) end |
#day ⇒ Object
79 80 81 |
# File 'opal/stdlib/date.rb', line 79 def day `#{self}._date.getDate()` end |
#month ⇒ Object
83 84 85 |
# File 'opal/stdlib/date.rb', line 83 def month `#{self}._date.getMonth() + 1` end |
#next ⇒ Object
87 88 89 90 91 |
# File 'opal/stdlib/date.rb', line 87 def next res = self.clone `res._date.setDate(#{self}._date.getDate() + 1)` res end |
#next_month ⇒ Object
93 94 95 96 97 |
# File 'opal/stdlib/date.rb', line 93 def next_month res = self.clone `res._date.add({months: 1})` res end |
#prev ⇒ Object
99 100 101 102 103 |
# File 'opal/stdlib/date.rb', line 99 def prev res = self.clone `res._date.setDate(#{self}._date.getDate() - 1)` res end |
#prev_month ⇒ Object
105 106 107 108 109 |
# File 'opal/stdlib/date.rb', line 105 def prev_month res = self.clone `res._date.add({months: -1})` res end |
#strftime(format = '') ⇒ Object
111 112 113 |
# File 'opal/stdlib/date.rb', line 111 def strftime(format = '') `#{self}._date.$strftime(#{format})` end |
#to_json ⇒ Object
122 123 124 |
# File 'opal/stdlib/date.rb', line 122 def to_json to_s.to_json end |
#to_s ⇒ Object
115 116 117 118 119 120 |
# File 'opal/stdlib/date.rb', line 115 def to_s %x{ var date = #{self}._date; return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate(); } end |
#wday ⇒ Object
130 131 132 |
# File 'opal/stdlib/date.rb', line 130 def wday `#{self}._date.getDay()` end |
#year ⇒ Object
134 135 136 |
# File 'opal/stdlib/date.rb', line 134 def year `#{self}._date.getFullYear()` end |