aboutsummaryrefslogtreecommitdiff
path: root/src/new_fields/DateField.ts
blob: 4f999e5e8f56a0a572bcd6e5d5ade40a989f5f48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { Deserializable } from "../client/util/SerializationHelper";
import { serializable, date } from "serializr";
import { ObjectField } from "./ObjectField";
import { Copy, ToScriptString } from "./FieldSymbols";
import { scriptingGlobal, Scripting } from "../client/util/Scripting";

@scriptingGlobal
@Deserializable("date")
export class DateField extends ObjectField {
    @serializable(date())
    readonly date: Date;

    constructor(date: Date = new Date()) {
        super();
        this.date = date;
    }

    [Copy]() {
        return new DateField(this.date);
    }

    toString() {
        return `${this.date.toISOString()}`;
    }

    [ToScriptString]() {
        return `new DateField(new Date(${this.date.toISOString()}))`;
    }
}

Scripting.addGlobal(function d(...dateArgs: any[]) {
    return new DateField(new (Date as any)(...dateArgs));
});