aboutsummaryrefslogtreecommitdiff
path: root/src/fields/DateField.ts
blob: 48106d978c5eb6a8f8f23de81f18cbe51dc20d9c (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
34
35
36
37
38
39
40
import { Deserializable } from "../client/util/SerializationHelper";
import { serializable, date } from "serializr";
import { ObjectField } from "./ObjectField";
import { Copy, ToScriptString, ToString } 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.toLocaleString()}`;
    }

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

    getDate() {
        return this.date;
    }
}

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