Find and convert objects key of key value into moment.utc()?

时间:2018-09-18 20:25:54

标签: javascript json datetime momentjs

I have JSON object and I am creating form with input fields using JSON objects(dynamically).

{student: {…}}

student:

Cob:"UNITED STATES OF AMERICA" Doa:"2018-09-15" Number:"16099999999"

What I am trying to do : I am trying to convert Doa into moment.utc() format. but this value is JSON object key value, I couldn't figure out how to call this value and convert.

for (var p in dataObject) {
            console.log(p);
            for (var k in dataObject[p]) {
                dataObject[p][k] = dataObject[p][k] === '' ? null : dataObject[p][k];
                var userInfo = dataObject[p][k];
                if ($('input[type="date"]')) {
                    userInfo = $(this).val();
                    var dateAndTime = moment.utc(userInfo).format('YYYY-MM-DDThh:mm:ss.SSSZ');
                    console.log(dateAndTime);
                    console.log(k + ' : ' + userInfo);
                }
            }
        }

Question : find and convert objects key of key value(which has input type ='date') into moment.utc() ?

1 个答案:

答案 0 :(得分:1)

我找到了使用正则表达式的解决方案。

for (var p in dataObject) {
                console.log(p);
                for (var k in dataObject[p]) {
                    dataObject[p][k] = dataObject[p][k] === '' ? null : dataObject[p][k];
                    var userInfo = dataObject[p][k];
                    if ((/^\d{4}[\-](\d{2})[\-](\d{2})$/g).test(userInfo)) {
                        dataObject[p][k] = moment.utc(userInfo).format('YYYY-MM-DDThh:mm:ss.SSSSSSSZ');
                    }
                }
            }