Gratzy
Gratzy

Reputation: 2908

Datetime showing up as GMT in view using knockout

I've got the following code being pulled from SQL Server with Linq:

UserList = (from u in userQuery
            select new UserViewModel
                       {
                            {...}
                            LastUpdate = u.LastUpdate,
                            {...}
                       }).AsQueryable();   

I put in a breakpoint and see the correct datetime that's getting pulled down and it is in my local timezone.

This is sent up to the view via JSON:

        <tr>
            <td>Last Updated</td>
            <td>
                <div data-bind="text: SelectedUser().LastUpdate" class="detailFields"></div>
            </td>
        </tr>

But now the datetime is showing as "GMT" instead of my local time.

Where am I going wrong?

Updated code:

Installed moment.js, imported and changed the line to:

<div data-bind="text: moment(SelectedUser().LastUpdate()).format('LLL')" class="detailFields"></div>

NOTE the () after the LastUpdate variable name...

Upvotes: 0

Views: 187

Answers (1)

Stefan
Stefan

Reputation: 17668

The invalid date of this line:

data-bind="text: moment(SelectedUser().LastUpdate).format('LLL')"

Is due to the fact that knockout.js want's it to be like:

data-bind="text: moment(SelectedUser().LastUpdate()).format('LLL')"

In general, when using and binding to knockout's observables, you need to put the () after them. More details on this subject can be found here: Knockoutjs binding to Property vs Property()

Upvotes: 1

Related Questions