1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-12 20:27:30 +08:00

Add Date.getYear

This commit is contained in:
Robert Krimen 2013-04-16 22:37:17 -07:00
parent a5ae851516
commit d5497fbe89
2 changed files with 10 additions and 0 deletions

View File

@ -22,6 +22,7 @@ func TestDate(t *testing.T) {
test(`new Date(1348616313).getTime()`, "1348616313")
test(`new Date(1348616313).toUTCString()`, "Fri, 16 Jan 1970 14:36:56 UTC")
test(`abc = new Date(1348616313047); abc.toUTCString()`, "Tue, 25 Sep 2012 23:38:33 UTC")
check(`abc.getYear()`, time.Year()-1900)
check(`abc.getFullYear()`, time.Year())
check(`abc.getUTCFullYear()`, 2012)
check(`abc.getMonth()`, int(time.Month())-1) // Remember, the JavaScript month is 0-based

View File

@ -357,6 +357,15 @@ func newContext() *_runtime {
},
// getTime, ...
"getTime", 0, builtinDate_getTime,
"getYear", 0, func(call FunctionCall) Value {
// Will throw a TypeError is ThisObject is nil or
// does not have Class of "Date"
date := dateObjectOf(call.thisObject())
if date.isNaN {
return NaNValue()
}
return toValue(date.Time().Local().Year() - 1900)
},
"getFullYear", 0, func(call FunctionCall) Value {
// Will throw a TypeError is ThisObject is nil or
// does not have Class of "Date"