0

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...

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Zonus
  • 2,313
  • 2
  • 26
  • 48
  • Do you have `moment.js` installed? – Stefan Aug 27 '18 at 21:26
  • Is that a knockout JS? I just scanned my installed JS files for this project and it doesn't appear that it's there. – Zonus Aug 27 '18 at 21:28
  • No, `moment.js` is a helper library to handle the date-time-of-javascript-hell... it' makes things easier. date-times can be hard, local time, GMT etc. Posting, displaying: they all seem to behave different. Take a look at this one: https://stackoverflow.com/questions/17148572/knockout-js-format-date-item – Stefan Aug 27 '18 at 21:30
  • I'm using this:
    but it's coming up as "Invalid Date" but it's the same as the article you linked. Can you see what I may be doing wrong?
    – Zonus Aug 27 '18 at 21:49
  • `Invalid Date`; it indicates that LastUpdate might have an invalid value. Have you checked that? – Stefan Aug 27 '18 at 21:50
  • I can remove the moment() from the datetime and it shows the datetime as GMT – Zonus Aug 27 '18 at 21:54
  • I needed a () after the LastUpdate... That fixed it! Thanks! did you want to make an answer so I can accept it? – Zonus Aug 27 '18 at 23:09

1 Answers1

1

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()

Stefan
  • 17,448
  • 11
  • 60
  • 79