diff --git a/global.go b/global.go index 00b21c8..7f8054e 100644 --- a/global.go +++ b/global.go @@ -499,8 +499,54 @@ func newContext() *_runtime { // setUTCHours // setDate // setUTCDate - // setMonth - // setUTCMonth + "setMonth", 1, func(call FunctionCall) Value { + date := dateObjectOf(call.thisObject()) + if date.isNaN { + return NaNValue() + } + value := call.Argument(0) + if value.IsNaN() { + date.SetNaN() + return NaNValue() + } + baseTime := date.Time().Local() + setTime := time_.Date( + baseTime.Year(), + dateToGoMonth(int(toInteger(value))), + baseTime.Day(), + baseTime.Hour(), + baseTime.Minute(), + baseTime.Second(), + baseTime.Nanosecond(), + baseTime.Location(), + ) + date.SetTime(setTime) + return date.Value() + }, + "setUTCMonth", 1, func(call FunctionCall) Value { + date := dateObjectOf(call.thisObject()) + if date.isNaN { + return NaNValue() + } + value := call.Argument(0) + if value.IsNaN() { + date.SetNaN() + return NaNValue() + } + baseTime := date.Time().UTC() + setTime := time_.Date( + baseTime.Year(), + dateToGoMonth(int(toInteger(value))), + baseTime.Day(), + baseTime.Hour(), + baseTime.Minute(), + baseTime.Second(), + baseTime.Nanosecond(), + baseTime.Location(), + ) + date.SetTime(setTime) + return date.Value() + }, "setFullYear", 1, func(call FunctionCall) Value { date := dateObjectOf(call.thisObject()) if date.isNaN { diff --git a/otto_test.go b/otto_test.go index 64d3e22..93d63c4 100644 --- a/otto_test.go +++ b/otto_test.go @@ -578,6 +578,10 @@ func TestDate(t *testing.T) { test(`new Date(12564504e5).toString()`, "Sun, 25 Oct 2009 06:00:00 UTC") test(`new Date(2009, 9, 25).toString()`, "Sun, 25 Oct 2009 00:00:00 UTC") test(`+(new Date(2009, 9, 25))`, "1.2564288e+12") + + test(`abc = new Date(12564504e5); abc.setMonth(9); abc.toString()`, "Sun, 25 Oct 2009 06:00:00 UTC") + test(`abc = new Date(12564504e5); abc.setMonth("09"); abc.toString()`, "Sun, 25 Oct 2009 06:00:00 UTC") + test(`abc = new Date(12564504e5); abc.setMonth("10"); abc.toString()`, "Wed, 25 Nov 2009 07:00:00 UTC") } func TestComparison(t *testing.T) { diff --git a/type_date.go b/type_date.go index d8b0f25..68ed4a4 100644 --- a/type_date.go +++ b/type_date.go @@ -88,7 +88,7 @@ func dateObjectOf(_dateObject *_object) *_dateObject { return _dateObject.Date } -// JavaScript is 0-based, Go is 1-based +// JavaScript is 0-based, Go is 1-based (15.9.1.4) func dateToGoMonth(month int) tme.Month { return tme.Month(month + 1) }