admin管理员组

文章数量:1388080

I'm using Knockout in bination with moment.js and C#. In C# i pass in a ISO formatted date by using: var jsonString = JsonConvert.SerializeObject(dataObject}, new IsoDateTimeConverter());

In my HTML file i'm doing the following to display my date in a formatted way:

<script type="text/javascript">
var viewModel = {};
$.getJSON(".txt", function(data) { 
viewModel.model = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);
});
</script>
<div>Hello</div>
<div>Time: <span data-bind="text: moment(model.Time).format('L')"></span></div>

I always get Invalid date but when i use:

<div>Time: <span data-bind="text: model.Time"></span></div>

it just displays correctly Time: 2014-08-25T09:49:00

Anyone an idea what i'm doing wrong?

I'm using Knockout in bination with moment.js and C#. In C# i pass in a ISO formatted date by using: var jsonString = JsonConvert.SerializeObject(dataObject}, new IsoDateTimeConverter());

In my HTML file i'm doing the following to display my date in a formatted way:

<script type="text/javascript">
var viewModel = {};
$.getJSON("http://www.test./jsonfile.txt", function(data) { 
viewModel.model = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);
});
</script>
<div>Hello</div>
<div>Time: <span data-bind="text: moment(model.Time).format('L')"></span></div>

I always get Invalid date but when i use:

<div>Time: <span data-bind="text: model.Time"></span></div>

it just displays correctly Time: 2014-08-25T09:49:00

Anyone an idea what i'm doing wrong?

Share Improve this question edited Aug 25, 2014 at 8:14 T.J. Crowder 1.1m200 gold badges2k silver badges1.9k bronze badges asked Aug 25, 2014 at 8:10 MichaelKMichaelK 8321 gold badge9 silver badges18 bronze badges 2
  • What does this have to do with C#? Or jQuery? Please tag appropriately. I've removed the irrelevant tags. – T.J. Crowder Commented Aug 25, 2014 at 8:14
  • object has been serialized to json by using " JsonConvert.SerializeObject(dataObject}, new IsoDateTimeConverter());" There are multiple ways, that's why i added C# – MichaelK Commented Aug 25, 2014 at 8:20
Add a ment  | 

1 Answer 1

Reset to default 7

fromJS creates observables, so model.Time is a function. So:

<span data-bind="text: moment(model.Time()).format('L')"></span>
<!-- Change is here --------------------^^                   -->

But the KO way would probably be to create a puted observable for that:

<script type="text/javascript">
var viewModel = {};
$.getJSON("http://www.test./jsonfile.txt", function(data) { 
    viewModel.model = ko.mapping.fromJS(data);
    viewModel.model.FormattedTime = ko.puted(function() {
        return moment(viewModel.model.Time()).format('L');
    });
    ko.applyBindings(viewModel);
});
</script>
<div>Hello</div>
<div>Time: <span data-bind="text: model.FormattedTime"></span></div>

Or better yet, your own binding: Live Example

ko.bindingHandlers.formattedTime = {
  update: function(element, valueAccessor) {
    $(element).text(moment(ko.unwrap(valueAccessor())).format('L'));
  }
};

Then:

<div>Time: <span data-bind="formattedTime: model.Time"></span></div>

本文标签: javascriptinvalid date when using knockoutjs and momentjsStack Overflow