2

I am using Visual Studio 2013 with C#.

I am calling one ActionResult method that returns the list of data from Ajax.

The problem here is I am getting date as "/Date(1460008501597)/".

I don't know how to convert it to display on form using javascript.

Please someone help me it's hard for me to solve.

5
  • Please share your controller code Commented Jul 18, 2016 at 13:07
  • Easiest method is to have your ActionResult return it in the final format as a string. Commented Jul 18, 2016 at 13:15
  • @freedomn-m Yes you are absolutely right but I want to post the whole model so I can't use any extra parameter and I have to set the date in the field of model itself so. Commented Jul 18, 2016 at 13:32
  • Yes - you add a read-only property to your model: public string DateValue { get { return this.Date.ToString("MM/dd/yyyy"); } } (it wasn't clear from the question if you had a model, so I left it intentionally vague) Commented Jul 18, 2016 at 13:38
  • ohh yes that was my mistake a good solution thanks @freedomn-m Commented Jul 18, 2016 at 13:39

3 Answers 3

5

You could use a function which return a date if the date string is in the wanted form or the value itself.

function getDateIfDate(d) {
    var m = d.match(/\/Date\((\d+)\)\//);
    return m ? (new Date(+m[1])).toLocaleDateString('en-US', {month: '2-digit', day: '2-digit', year: 'numeric'}) : d;
}

console.log(getDateIfDate("/Date(1460008501597)/"));
console.log(getDateIfDate('abc'));

Sign up to request clarification or add additional context in comments.

4 Comments

@NinaScholz general function could be better thanks. Can you help me a little bit I want to display date like "MM/DD/YY" is it possible in funtion itself or I have to do some operation when I get returned value?
@NinaScholz Hello can you just pleas check my answer is it right so I can use it!
you could use toLocaleDateString with some options to shape the appearance.
Ohhh yes amazing but it adds one date ahead means I my date is 3 it return 4!
5

In pure javascript, you can do this:

var date = new Date(Number("/Date(1460008501597)/".replace(/\D/g, '')));

Explanation:

new Date(
    Number(
        "/Date(1460008501597)/".replace(/\D/g, '') // Removes all non digit characters
    ) // Cast it to numeric
) // Creates a new Date object with the resultant number

Now, for more accurate solution for your problem, like display the date w/o javascript(which I think would be better choice), you can improve your question with more detail/information/code.

2 Comments

Exactly what I am looking for thanks @DontVoteMeDown Can you help me little more I want to display date in MM/dd//yy format Can i do it from date variable itself?
Thanks for your suggestion but I can't use any new js as per requirement for my project can you give me suggestion to do that using javascript?
0

I have done it like this:(Is it ok)

function getDateIfDate(d) {
        var m = d.match(/\/Date\((\d+)\)\//);
        return (new Date(+m[1])).getMonth() + "/" + (new Date(+m[1])).getDate() + "/" + (new Date(+m[1])).getFullYear();
        //return m ? (new Date(+m[1])) : d;
    }

2 Comments

if it works for you, then it's ok. but getMonth returns 0-11 and not 1-12.
I suggested you something really close to this. In the comment(which I deleted) I did like you, but in a simple way. Like, you don't need to create the Date object everytime, why won't you store it in a var? E.g.: var date = new Date(+d.match(/\/Date\((\d+)\)\//)[1]).. then date.getMonth()..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.