vini
vini

Reputation: 4742

How to run javascript function in Razor MVC .net

        <script type="text/javascript">
                @if (@Model.Invoice.InvoiceDate != null)
                {

                    <text>
                    function UTCToLocalTime(date) {
                        var time = new Date(Date.parse(date));
                        var timeOffset = -((new Date()).getTimezoneOffset() / 60);
                        time.setHours(d.getHours() + timeOffset);
                        return time;
                    }


                    var date = @Model.Invoice.InvoiceDate
                    UTCToLocalTime(date);
                    </text>




                }
                </script>

I am printing the above invoice and its open in a window. I am getting a Unexpected number error.

enter image description here

Upvotes: 0

Views: 259

Answers (2)

Alexandru Chichinete
Alexandru Chichinete

Reputation: 1183

Try to use:

var date = '@Model.Invoice.InvoiceDate';

Since Date.parse is used to parse a string in javascript.

More info about Date.parse here.

Upvotes: 0

Mackan
Mackan

Reputation: 6271

That sequence of numbers is not recognized by JavaScript. If you put it in quotes, it'll be treated like a string instead and be valid:

var date = '@Model.Invoice.InvoiceDate';

Or, if you want a date object (and have a valid date for it!):

var date = new Date('@Model.Invoice.InvoiceDate');

Upvotes: 3

Related Questions