0

I want to push ASP.NET Model DateTime values into an array to be used within the Google Chart API. I get an error saying I'm missing an ')' when I use the following:

    var dates = [];
    @foreach (var s in Model.Scores)
    {
        //it works fine here
        @:dates.push(parseFloat(@s.Score));
        //but causes an error here
        @:dates.push(new Date(@s.Date));
    }

I've also tried

@:dates.push(String(@s.Date));

and

@:dates.push(String(@s.Date.ToString()));

and

@:dates.push(String(@s.Date.Date));

When I pass it a number instead of a DateTime it works fine:

    @for (var i = 1; i <= Model.Scores.Count(); i++)
    {
        @:count.push(@i);
    }
coolhand
  • 1,876
  • 5
  • 25
  • 46
  • Relevant https://stackoverflow.com/questions/1016847/converting-net-datetime-to-json – charlietfl May 29 '18 at 01:09
  • @charlietfl I tried that and I get the same error – coolhand May 29 '18 at 01:26
  • what does the generated source look like? Also is `var dates` supposed to be a javascript variable or asp one? – charlietfl May 29 '18 at 01:26
  • @charlietfl `dates` is supposed to be JavaScript. The generated code looks shows a `DateTime` value pushed into the js array – coolhand May 29 '18 at 01:28
  • OK but those Datetime outputs are not valid in javascript. You need to extract the timestamp from them. I'm assuming they look like `Date(2004005000)` – charlietfl May 29 '18 at 01:29
  • @charlietfl in this case they look like `Date(05/28/2018 12:00:00)` or thereabouts (I didn't copy directly). How do I get it to the format needed? – coolhand May 29 '18 at 01:53

0 Answers0