soulim / ember-cli-bootstrap-datepicker

Datepicker component for Ember CLI
http://sul.im/ember-cli-bootstrap-datepicker
MIT License
68 stars 57 forks source link

Selected date is one day behind on the payload #91

Open mongeta opened 7 years ago

mongeta commented 7 years ago

I have an attribute as date_started: DS.attr('date'),

In my template, I have this: {{bootstrap-datepicker value=model.date_started class="form-control" todayBtn='linked' required=true}}

So far so good, the datepicker opens, I select for example 31/12/2016 (dd/mm/yyyy) and the binded input has the correct date on it.

This is what I see on the console: Sat Dec 31 2016 00:00:00 GMT+0100 (CET)

But, when I save() the model, the output payload is this: "date-started"=>2016-12-30T23:00:00.000Z, this is one day behind

Ember: 2.10.1 Ember Data: 2.10.0 jQuery: 3.1.1 ember-cli-bootstrap-datepicker: 0.5.6

I'm using JSONAPI, and no custom serializers.

If I understand correctly, this addon uses Date Javascript Object, and Ember also use it when adding the 'date' name.

What I'm doing wrong? I've read here in some issues that you have to use computed propertys, getters/setters, but only in the case you want a string format, but not if you're using the custom Date js object.

I'm aware about the js dates and utc conversions, but surely this is a common scenario with a common solution ...

thanks,

mongeta commented 7 years ago

The workaround, or maybe its not a workaroun, it is just the way how to do things in Ember/js world ;-)

I have created a DS.Transform called bs-date:

import moment from 'moment';

export default DS.Transform.extend({
    serialize: function(value) {
        return value ? new Date(moment(value).format('YYYY-MM-DD')) : null;
    },

    deserialize: function(value) {
        return new Date(value);
    }
});

And in the model, just:

date_started: DS.attr('bs-date'),

With this transform all works perfect, at this moment ;-)

thanks,