KnockoutJS - 拆分字符串

时间:2015-10-06 01:57:29

标签: knockout.js

应该如此简单的东西一直让我在Knockoutjs中疯狂。只是为了分割传入的字符串。

我有来自日历的opts.value()并希望将其拆分以删除时间,但它会一直返回“不是函数”。并且不喜欢.split。有什么想法吗?

self.ShortDate = ko.computed(function () {
        return self.opts.value().split(" ",4);
    }, self);

以下是opts.value返回的字符串“Wed Oct 14 2015 00:00:00 GMT + 1300(新西兰标准时间)”

很抱歉不包括VM,这是https://github.com/MakerStudios/ko-calendar

中的相关部分
self.opts = {
        value: ko.observable(),
        DateFromIn: ko.observable(),
        current: new Date(),
        deselectable: true,
        showCalendar: true,
        showToday: true,
        showTime: false,
        showNow: false,
        militaryTime: false,
        min: null,
        max: null,
        autoclose: false,

1 个答案:

答案 0 :(得分:2)

根据documentation of the ko-calendarvalue选项为ko.observable([Date Object]),因此它会存储Date个对象而不是string

因此,您需要先将其转换为字符串,然后才能进行拆分:

self.ShortDate = ko.computed(function () {
    return self.opts.value().toString().split(" ",4);
}, self);

但是,格式Date对象有更好的选项:How to format a JavaScript date