JSON Date To Date Vice-versa


Hi Friends, This is Lakshman here.

You may also able to view on GitHub by Click Here

Most of the people Keep on checking how to convert the JSON Date to Javascript Date viceversa.

Let First write the function to get JSON Date from Date

* Javascript

    function getJSONDate() {
        var dt = new Date();
        var newDate = new Date(Date.UTC(dt.getFullYear(), dt.getMonth(),    dt.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds(), dt.getMilliseconds()));

        var JSONDate = '/Date(' + newDate.getTime() + ')/';

        return JSONDate;
    }

* AngularJS
    vm = this;

    vm.getJsonDate = function() {
        var dt = new Date();
        var newDate = new Date(Date.UTC(dt.getFullYear(), dt.getMonth(),    dt.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds(), dt.getMilliseconds()));

        var JSONDate = '/Date(' + newDate.getTime() + ')/';

        return JSONDate;
   }

* Angular
    getJSONDate() {
        const dt = new Date();
        const newDate = new Date(Date.UTC(dt.getFullYear(), dt.getMonth(),    dt.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds(), dt.getMilliseconds()));

        const JSONDate = '/Date(' + newDate.getTime() + ')/';

        return JSONDate;
    }

If you are going to get JSON Date of Existing Date. Please pass the Date for example
    function getJSONDate(dt) {        
        var newDate = new Date(Date.UTC(dt.getFullYear(), dt.getMonth(),    dt.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds(), dt.getMilliseconds()));

        var JSONDate = '/Date(' + newDate.getTime() + ')/';

        return JSONDate;
    }

Please convert the function based on the technology.

Now let convert **JSON Date to Date**

* Javascript
    function getDateFrmJSON(jsonDate) {
        var fulldate = "";
        if (jsonDate !== '') {
            var regex = /-?\d+/;</p>
            var matches = regex.exec(jsonDate);</p>
            fulldate = new Date(parseInt(matches[0], null));
        }

        return fulldate;
    }


* AngularJS
    vm = this;

    vm.getDateFrmJSON = function(jsonDate) {
        var fulldate = "";
        if (jsonDate !== '') {
            var regex = /-?\d+/;</p>
            var matches = regex.exec(jsonDate);</p>
            fulldate = new Date(parseInt(matches[0], null));
        }

        return fulldate;
   }

* Angular
    getDateFrmJSON(jsonDate) {
       const fulldate = "";
        if (jsonDate !== '') {
            const regex = /-?\d+/;</p>
            const matches = regex.exec(jsonDate);</p>
            fulldate = new Date(parseInt(matches[0], null));
        }

        return fulldate;
    }

Comments

Popular posts from this blog

Learn to be a Phoenix

Good Listener

No Data No Scale