简体   繁体   中英

How to convert json date format to normal date format. Ex:'/Date(1388412591038)/' to '12-30-2013'?

This is my json generated at client side. I am posting this json to the server. But the StartDate and EndDate are not being converted. Please help.

[
    {
        "GoalTitle": "Competancy Goal",
        "Weightage": 30.5,
        "StartDate": "/Date(1388412173070)/",
        "EndDate": "/Date(1419948173070)/",
        "Status": 0,
        "editing": false,
        "lstSubGoals": [
            {
                "GoalTitle": "Competancy Goal - Sub goal",
                "Weightage": 31.5,
                "StartDate": "/Date(1388412173070)/",
                "EndDate": "/Date(1419948173070)/",
                "Status": 0,
                "editing": false,
                "lstSubGoals": []
            }
        ]
    },
    {
        "GoalTitle": "Strategy Goal",
        "Weightage": 60.5,
        "StartDate": "/Date(1388412173070)/",
        "EndDate": "/Date(1419948173070)/",
        "Status": 1,
        "editing": false,
        "lstSubGoals": []
    }
]

Add below function in your JS file:

function ConvertJsonDateString(jsonDate) {
        var shortDate = null;
        if (jsonDate) {
            var regex = /-?\d+/;
            var matches = regex.exec(jsonDate);
            var dt = new Date(parseInt(matches[0]));
            var month = dt.getMonth() + 1;
            var monthString = month > 9 ? month : '0' + month;
            var day = dt.getDate();
            var dayString = day > 9 ? day : '0' + day;
            var year = dt.getFullYear();
            shortDate = monthString + '-' + dayString + '-' + year;
        }
        return shortDate;
    };

Then you can use it like:

<script>
    var jsonDate = '/Date(1388412173070)/';
    var date = ConvertJsonDateString(jsonDate);
    alert(date)  // the result will be 12/30/2013
</script>

See result here: http://jsfiddle.net/lin/WrcC8/

function parseJsonDate(jsonDate) {
    var offset = new Date().getTimezoneOffset() * 60000;
    var parts = /\/Date\((-?\d+)([+-]\d{2})?(\d{2})?.*/.exec(jsonDate);
    if (parts[2] == undefined) parts[2] = 0; 
    if (parts[3] == undefined) parts[3] = 0; 
    return new Date(+parts[1] + offset + parts[2]*3600000 + parts[3]*60000); 
}

Reference from: here

JSFIDDLE DEMO

var num = "/Date(1388412591038)/".match(/\d+/g); //regex to extract numbers 
var date = new Date(parseFloat(num)); //converting to date
console.log(date.getMonth() + 1 + "-" + date.getDate() + '-' + date.getFullYear());

Result in console : 12-30-2013

@dineshd87 It's very simple you can do it in jquery as:

var date = new Date(//the milliseconds here);
var dateString = date.getDate() + "/" + date.getMonth() + "/" + date.getFullYear();
var StartDateServer = StartDate;
var parsedDate = new Date(parseInt(StartDateServer.substr(6)));
var finalDate = parsedDate.toLocaleDateString(); //result as mm/dd/yyyy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM