aboutsummaryrefslogtreecommitdiff
path: root/src/fields/DateField.ts
blob: 2ea619bd94e3e12739ed01982a21175fe7546f04 (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, ScriptingGlobals } from '../client/util/ScriptingGlobals';

@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;
    }
}

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