BrightonDev
BrightonDev

Reputation: 435

KnockOutJS - Binding to dates returned from the server

How do I work with dates in KnockoutJS? I'm using the code below to render the textboxes with formatted dates from the server model:

var viewModel = {
 startDate: ko.observable(@(Html.Raw(Model.Holiday.StartDate.ToString("dd/MM/yyyy")))),
 endDate: ko.observable(@(Html.Raw(Model.Holiday.EndDate.ToString("dd/MM/yyyy")))), 
}

<input name="Holiday.StartDate" data-bind="value: startDate" class="inputdate" />
<input name="Holiday.EndDate" data-bind="value: endDate" class="inputdate" />

However what is being renedred looks like below:

StartDate textbox renders: 0.0007182717277197635

EndDate textbox renders: 0.0011050334272611746

What am I missing?

Upvotes: 2

Views: 1503

Answers (1)

RP Niemeyer
RP Niemeyer

Reputation: 114792

If you want to get a string representing your date into your UI, then you would want to put quotes around

var viewModel = {
 startDate: ko.observable("@(Html.Raw(Model.Holiday.StartDate.ToString("dd/MM/yyyy")))"),
 endDate: ko.observable("@(Html.Raw(Model.Holiday.EndDate.ToString("dd/MM/yyyy")))"), 
}

Currently it is putting something like ko.observable(13/9/2011) and doing the math on it.

Upvotes: 1

Related Questions