0

I have a JavaScript function within an MVC Razor View. The View is passed a model which contains two strings to represent UK dates, e.g., dateFrom 01/03/2018 and dateTo 31/03/2018 .

<script type="text/javascript">
$(document).ready(function () {

    function LoadDatatable(to, from)
    {
        alert(to);
        alert(from);
    }

    LoadDatatable(@Model.dateFrom, @Model.dateTo);

});

These date strings are then passed to the function LoadDatatable, however, the dates are completely changed when this happens. The screenshot below shows the alert prompt for the dateTo string which was passed into the function as 31/03/2018.

enter image description here

Can someone please help? I need the function not to change the passed date strings.

TylerH
  • 20,799
  • 66
  • 75
  • 101
tcode
  • 5,055
  • 19
  • 65
  • 124
  • 1
    You just discovered the difference between a text literal ... and a _mathematical operation_ ... – CBroe Apr 05 '18 at 14:13
  • 1
    _“I need the function not to change the passed date strings?”_ - you just need to actually pass strings, which so far you simply didn’t. – CBroe Apr 05 '18 at 14:15
  • 2
    `LoadDatatable('@Model.dateFrom', '@Model.dateTo');` – Rory McCrossan Apr 05 '18 at 14:16
  • Hint: take the date, e.g. `1/4/2018` and type it as-is into Windows Calculator, or into Excel with an `=` before it. Also, doing a bit of Browser > View Source would have revealed the problem rather quickly... – Peter B Apr 05 '18 at 14:16
  • It's been a long day - silly mistake - thanks folks. – tcode Apr 05 '18 at 14:23

1 Answers1

1

Just pass the date like this, It should work.

function LoadDatatable(to, from)
{
    alert(to);
    alert(from);
}
LoadDatatable('@Model.dateFrom', '@Model.dateTo');

Please check here more on this.

Smit Patel
  • 2,992
  • 1
  • 26
  • 44