Skip to content
This repository has been archived by the owner on Dec 26, 2019. It is now read-only.

Selected date is one day behind on the payload #91

Open
mongeta opened this issue Dec 29, 2016 · 1 comment
Open

Selected date is one day behind on the payload #91

mongeta opened this issue Dec 29, 2016 · 1 comment

Comments

@mongeta
Copy link

mongeta commented Dec 29, 2016

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
Copy link
Author

mongeta commented Dec 30, 2016

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,

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant