Adding 1 to Date while binding using KnockoutJS I am using KnockoutJS to bind some values. I have a date value TimeByDay that I bind like this:

&lt;span data-bind="text: new Date(TimeByDay)"&gt;&lt;/span&gt;


But I would also like to transform the date on a different location and simply add a day to the TimeByDay value.

I can do it like this:

&lt;span data-bind="text: new Date(new Date(TimeByDay).setDate(new Date(TimeByDay).getDate() + 1))"&gt;&lt;/span&gt;


Is there a prettier way to do this? Maybe some feature in KnockoutJS that I am missing?

(The reason I am encapsulating both values in a new Date() construct is that I also apply formatting to the date.)

ViewModel:

$(document).ready(function () {

    var serverData = $.getJSON("../../_api/");
    serverData.done(function (data) {
        ko.applyBindings(new EngagementsList(data.value));
    });

});

function EngagementsList(engagementList) {
    var self = this;
    self.entries = engagementList;
}


HTML:

&lt;span data-bind="foreach: entries"&gt;
  &lt;span data-bind="text: new Date(TimeByDay)"&gt;&lt;/span&gt;
  &lt;span data-bind="text: new Date(new Date(TimeByDay).setDate(new Date(TimeByDay).getDate() + 1))"&gt;&lt;/span&gt;
&lt;/span&gt;
One way would be to move the date display logic to your view model.

function vm(){
    var self= this;

    self.TimeByDay = ko.observable(new Date("December 17, 1995 03:24:00"));

    // fn extention of ko.observable        
    ko.observable.fn.AddOneDay = function(){
        return new Date(this().setDate(this().getDate()+1))
    };

    self.AddOneDay = function(TimeByDay){
        return new Date(TimeByDay().setDate(TimeByDay().getDate()+1))
    };
}


And then in your view you can have

&lt;span data-bind="text: TimeByDay"&gt;&lt;/span&gt;
&lt;br/&gt;
&lt;span data-bind="text: AddOneDay(TimeByDay)"&gt;&lt;/span&gt;


Or

&lt;span data-bind="text: TimeByDay.AddOneDay()"&gt;&lt;/span&gt;


Use the extention fn method if your application requires 1 day added to date frequently. Read this for more.

Jsfiddle: https://jsfiddle.net/newuserjs/oLh6u67z/4/