Class: Date

Inherits:
Object show all
Defined in:
opal/stdlib/date.rb,
opal/stdlib/json.rb

Class Method Summary collapse

Instance Method Summary collapse

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

.todayObject



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._isNumber) {
      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._isNumber) {
      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

#<=(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 Also known as: eql?



90
91
92
93
94
95
# File 'opal/stdlib/date.rb', line 90

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

#as_jsonObject



170
171
172
# File 'opal/stdlib/json.rb', line 170

def as_json
  to_s
end

#cloneObject



99
100
101
# File 'opal/stdlib/date.rb', line 99

def clone
  Date.wrap(`new Date(#@date.getTime())`)
end

#dayObject



103
104
105
# File 'opal/stdlib/date.rb', line 103

def day
  `#@date.getDate()`
end

#monthObject



107
108
109
# File 'opal/stdlib/date.rb', line 107

def month
  `#@date.getMonth() + 1`
end

#nextObject



111
112
113
114
115
# File 'opal/stdlib/date.rb', line 111

def next
  res = self.clone
  `res.date.setDate(#@date.getDate() + 1)`
  res
end

#next_monthObject



117
118
119
120
121
122
123
124
125
# File 'opal/stdlib/date.rb', line 117

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

#prevObject



127
128
129
130
131
# File 'opal/stdlib/date.rb', line 127

def prev
  res = self.clone
  `res.date.setDate(#@date.getDate() - 1)`
  res
end

#prev_monthObject



133
134
135
136
137
138
139
140
141
# File 'opal/stdlib/date.rb', line 133

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



143
144
145
# File 'opal/stdlib/date.rb', line 143

def strftime(format = '')
  `#@date.$strftime(#{format})`
end

#to_jsonObject



166
167
168
# File 'opal/stdlib/json.rb', line 166

def to_json
  to_s.to_json
end

#to_sObject



147
148
149
150
151
152
153
154
# File 'opal/stdlib/date.rb', line 147

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

#wdayObject



156
157
158
# File 'opal/stdlib/date.rb', line 156

def wday
  `#@date.getDay()`
end

#yearObject



160
161
162
# File 'opal/stdlib/date.rb', line 160

def year
  `#@date.getFullYear()`
end