0

I have a c# model that I am parsing into Json in my Razor view like this:

@Html.Raw(Json.Encode(Model.myModel))

However part of myModel is a date that I need to insatiate as a Javascript date like so:

new Date(yyyy-MM-dd)

Without creating the new javascript date the date is getting parsed and read as a string.

How can I accomplish this?

user3839756
  • 793
  • 1
  • 9
  • 22
  • 1
    Not clear what your asking. I assume you mean the your getting something like `/Date(1517529452191)/`? –  Feb 02 '18 at 00:00
  • Yes my output is similar to that, what im looking for is an output like this: Date(yyyy-MM-DD) if I format the date before adding it to my object I get an output like this "yyyy-MM-DD" – user3839756 Feb 02 '18 at 00:10
  • Then in your view, convert it to a `Date` using `var date = new Date(parseInt(yourValue.substr(6)));` –  Feb 02 '18 at 00:12
  • ^^^ should do it. Here is an explanation if you are interested. https://stackoverflow.com/a/42075212/40521 – Shyju Feb 02 '18 at 00:13
  • I dont have access to every instance of date as I am using this to create the Json: @Html.Raw(Json.Encode(Model.myModel)) – user3839756 Feb 02 '18 at 00:30

1 Answers1

0

You can try this:

var GetDateString= JSON.stringify(@Html.Raw(Json.Encode(Model.myModel)));

This JSON.stringify make any value as String format.

Just to make sure that this Model.myModel is a true value and not a list of model itself.

Vijunav Vastivch
  • 4,153
  • 1
  • 16
  • 30