1

Something that should be so simple, has been driving me nuts in Knockoutjs. Just to split an incoming string.

I have opts.value() from the calendar coming in and want to split it to remove the time, but it keeps returning "not a function". and doesn't like .split. Any ideas?

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

Here is the string that is returned by opts.value "Wed Oct 14 2015 00:00:00 GMT+1300 (New Zealand Standard Time)"

Sorry for not including the VM, here is the relevant part from the 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,
netchicken
  • 289
  • 2
  • 6
  • 18
  • share up complete relevant part of vm . Try `self.opts().value.split(' ',4)` (i'm guessing - opts is observable inside to it you have plane js variable) – super cool Oct 06 '15 at 04:13
  • I have added more info Self.Opts.value it comes from here https://github.com/MakerStudios/ko-calendar – netchicken Oct 06 '15 at 04:38

1 Answers1

2

According to the documentation of the ko-calendar the value option is a ko.observable([Date Object]) so it stores a Date object not a string.

So you need to convert it first to a string then you can do the split:

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

However there are better options exist for formatting Date objects: How to format a JavaScript date

Community
  • 1
  • 1
nemesv
  • 133,215
  • 15
  • 395
  • 348