Class: Date

Inherits:
Object show all
Defined in:
opal/stdlib/date.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



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

.todayObject



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_jsonObject



126
127
128
# File 'opal/stdlib/date.rb', line 126

def as_json
  to_s
end

#cloneObject



75
76
77
# File 'opal/stdlib/date.rb', line 75

def clone
  Date.wrap(`new native_date(#{self}._date.getTime())`)
end

#dayObject



79
80
81
# File 'opal/stdlib/date.rb', line 79

def day
  `#{self}._date.getDate()`
end

#monthObject



83
84
85
# File 'opal/stdlib/date.rb', line 83

def month
  `#{self}._date.getMonth() + 1`
end

#nextObject



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_monthObject



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

#prevObject



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_monthObject



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_jsonObject



122
123
124
# File 'opal/stdlib/date.rb', line 122

def to_json
  to_s.to_json
end

#to_sObject



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

#wdayObject



130
131
132
# File 'opal/stdlib/date.rb', line 130

def wday
  `#{self}._date.getDay()`
end

#yearObject



134
135
136
# File 'opal/stdlib/date.rb', line 134

def year
  `#{self}._date.getFullYear()`
end