Datetime UTC parse into local time

Hello,

How do I parse this UTC format into local browser in the Datepicker view?

Best,
Linda

Tagged:

Comments

  • try to use custom format
    format: value => value.toUTCString()
    https://snippet.webix.com/jdzysgj9

  • Hi @integral, this doesn't seem to work for me. The output I get in the json is 2019-10-09T16:06:00.000Z and I'm loading in the data via a url.

    https://snippet.webix.com/jdzysgj9

  • edited October 10

    The output I get in the json is 2019-10-09T16:06:00.000Z
    date string ending with Z means UTC format.
    AFAIK date's JSON output is always UTC, unless you override it.

    if you want to convert UTC string coming from server into date object on client side, then you need to define date converting reviver.

    (function (JSON) {
        const dateISO = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/i;
    
        const parser = JSON.parse;
        const registeredRevivers = {};
        let reviverID = Date.now();
    
        function getId(id?: string) {
            return id || (reviverID++).toString(32);
        }
        function registerReviver(reviver: (name: any, value: any) => any, id?: string) {
            id = getId(id);
            registeredRevivers[id] = reviver;
            return id;
        }
        function unregisterReviver(id: string | number) {
            delete registeredRevivers[id];
        }
        function jsonReviver(reviver: (key: any, value: any) => any) {
            return function () {
                const value = arguments[1];
                let temp = value;
                if (reviver) {
                    temp = reviver.apply(this, arguments);
                }
                if (temp === value) {
                    for (const key in registeredRevivers) {
                        if (registeredRevivers.hasOwnProperty(key)) {
                            temp = registeredRevivers[key].apply(this, arguments)
                            if (temp !== value) {
                                break;
                            }
                        }
                    }
                }
                return temp;
            }
        }
        function jsonParser(text: string, reviver: (key: any, value: any) => any) {
            return parser.call(this, text, jsonReviver(reviver))
        }
        function dateTimeReviver(name: any, value: any) {
            if ((typeof value == "string") && dateISO.test(value)) {
                value = new Date(value);
            }
            return value;
        }
    
        registerReviver(dateTimeReviver, "dateTime");
    
        JSON.register = registerReviver;
        JSON.unregister = unregisterReviver;
        JSON.parse = jsonParser;
    }(JSON));
    

    this is my implementation in typescript

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!