This is a migrated thread and some comments may be shown as answers.

Handling dates

8 Answers 859 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
sitefinitysteve asked on 22 May 2012, 11:18 PM
My model has date objects dotted all around it, what's the preferred way to handle the formatting of those into a readable format?

I'm not sure how to get the values into a function to return the formatted Date object back...I know I can't databind a format directly, so how do I pass in the data value to a function so it returns what format I want.

data-bind="text: getDate(selectedBlock.data.EndDate)"

I've tried things like the above, but doesn't do anything (so clearly it's wrong)

8 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 25 May 2012, 09:15 AM
Hi Steve,

 You cannot have *any* executable code in the data-bind attribute. You can only specify field and function names which correspond to model fields/functions. You can find more info in the ObservableObject help topic: http://www.kendoui.com/documentation/framework/mvvm/observableobject.aspx 

 To format a date you need to create a calculated field - a function which will return date formatted the right way.

Regards,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 25 May 2012, 08:05 PM
Can you elaborate on that though?  If you have a complex model populated by json...can you get away somehow with a single function or do you need to have one for every date property?
0
Atanas Korchev
Telerik team
answered on 28 May 2012, 08:43 AM
Hello,

 You need to have one for every date property.

Regards,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 28 May 2012, 06:31 PM
That's a horrible solution :/ ...for a complex model, I guess I just need it to do date formatting anyway, this works better I think

//Date Binding
kendo.data.binders.date = kendo.data.Binder.extend({
    init: function (element, bindings, options) {
        kendo.data.Binder.fn.init.call(this, element, bindings, options);
 
        this.dateformat = $(element).data("dateformat");
    },
    refresh: function () {
        var data = this.bindings["date"].get();
        if (data) {
            var dateObj = new Date(data);
            $(this.element).text(kendo.toString(dateObj, this.dateformat));
        }
    }
});
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 28 May 2012, 08:47 PM
Could there not be a property or a kendo method to convert any values that pass a parseDate in the model into JS dates?
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 29 May 2012, 01:20 AM
Crap, no I think there's even issues with a custom binder...

If I'm doing this:
<li>Last Updated:<span data-bind="date: selectedBlock.LastUpdated" data-dateformat="dddd MMM dd, yyyy hh:mmtt"></span></li>

So binding to a child property of an object...

The refresh of the custom binder isn't called when the selectedBlock changes and the data is bound

Where if I change it to just this
<li>Last Updated:<span data-bind="text: selectedBlock.LastUpdated"></span></li>

It works as expected

Bug?
0
Atanas Korchev
Telerik team
answered on 30 May 2012, 07:16 AM
Hi,

 Looks ok to me: http://jsfiddle.net/korchev/urAPV/1/ 

Regards,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Maciej
Top achievements
Rank 1
answered on 19 Jul 2017, 08:16 PM
In case someone stumbles upon this. Here's the answer: http://docs.telerik.com/kendo-ui/framework/mvvm/bindings/text#text-formatting
Tags
MVVM
Asked by
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Answers by
Atanas Korchev
Telerik team
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Maciej
Top achievements
Rank 1
Share this question
or