0

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": []
    }
]
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
dineshd87
  • 141
  • 1
  • 2
  • 9
  • 2
    You should use Json.NET instead of the default javascript serializer. – EfrainReyes Dec 30 '13 at 14:29
  • [This](http://stackoverflow.com/questions/1244094/converting-json-results-to-a-date) may help you! – Dhaval Marthak Dec 30 '13 at 14:29
  • I believe Json.NET is in the server. Once i post the above json string to the server, in the server i receive it as '1/1/0001 12:00:00 AM'. – dineshd87 Dec 30 '13 at 14:31
  • 1
    Are you using JSON.stringify before passing this off to the server? Also, if this is generated client side, why not just generate a format the server can understand? – dherman Dec 30 '13 at 17:31

5 Answers5

2
var StartDateServer = StartDate;
var parsedDate = new Date(parseInt(StartDateServer.substr(6)));
var finalDate = parsedDate.toLocaleDateString(); //result as mm/dd/yyyy
Md. Asaduzzaman
  • 137
  • 1
  • 12
1

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/

Lin
  • 15,078
  • 4
  • 47
  • 49
  • Thanks for the help. But, do i need to loop through json and find which are of date type and call the function?? – dineshd87 Dec 30 '13 at 14:35
  • @dineshd87 Yes. It looks like `StartDate` & `EndDate` are the only ones which are Date type. – Venkata Krishna Dec 30 '13 at 14:46
  • @Krishna Yes, but my model is recursive. The lstSubGoals will again have subgoals in that. It goes on like that. Isn't there any way where i just pass the whole json string and get back with the formatted date. – dineshd87 Dec 30 '13 at 14:53
  • @dineshd87 - before you convert it to date, you have to know exactly what node should be converted(convertible?). Extract whatever string you want & use the above code to get the date format. – Venkata Krishna Dec 30 '13 at 15:00
1

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

Venkata Krishna
  • 14,926
  • 5
  • 42
  • 56
  • Thanks for the help. But, do i need to loop through json and find which are of date type and call the function?? – dineshd87 Dec 30 '13 at 14:43
  • 2
    @dineshd87: There is no JSON Date type. "/Date(1388412173070)/" is just a string. You would have to know in advance which string values contained extractable dates. – bobince Dec 30 '13 at 14:48
  • @dineshd87 - please don't forget to upvote all helpful answer and accept one as an answer :) – Venkata Krishna Dec 30 '13 at 15:16
  • @Krishna why date.getMonth() is added by 1 and other like date.getDate() and date.getFullYear() not added any number? – S.Pradeep Jun 07 '20 at 16:54
  • @S.Pradeep - getMonth() is zero index based, where as others are not. That means, January is represented by 0 and December is represented by 11. – Venkata Krishna Jun 08 '20 at 13:10
0
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

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Uniruddh
  • 4,427
  • 3
  • 52
  • 86
0

@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();
Ali Shahzad
  • 5,163
  • 7
  • 36
  • 64