Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow mat-datepicker to return a string instead of a date object. #16010

Open
CharlieGreenman opened this issue May 13, 2019 · 6 comments
Open

Allow mat-datepicker to return a string instead of a date object. #16010

CharlieGreenman opened this issue May 13, 2019 · 6 comments

Comments

@CharlieGreenman
Copy link

@CharlieGreenman CharlieGreenman commented May 13, 2019

Please describe the feature you would like to request.

Allow for the mat-datepicker to return a string, instead of a Date object.

What is the use-case or motivation for this proposal?

I am currently attempting to use mat-datepicker, with the Angular Form Builder. The only issue, is that mat-datepicker will return the selected value as a Date object instead of a string. This does not allow for the value to be immediately passed in from the mat-datepicker into a formGroup, and then sent as a request to the server without first converting it to a string. In an app that heavily uses date-picker it requires a new component to be built on top of mat-datepicker.

Is there anything else we should know?

The following is a stackblitz, which re-iterates point that the mat-datepicker returns an object, instead of the expected string. You will have to open up console in order to see value.

If you have any recommendations on how to provide a value from a module side of things, so that mat-datepicker returns a string instead of a date object, it would be very much so appreciated. Thank you.

@Qocotzxin
Copy link

@Qocotzxin Qocotzxin commented May 17, 2019

Hi! I have a problem changing the business logic of datepicker and I came across with the issue you mention. As a simple workaround if you use the moment-date-adapter, you can easily access the value as a string by using String(formValue.creationData().input). You are gonna have to type the value you get as Moment (the interface) so you can get the methods. Hope it helps!

Sorry the wrong tag before!

@CharlieGreenman
Copy link
Author

@CharlieGreenman CharlieGreenman commented May 21, 2019

@Qocotzxin thank you for commenting! If I may, I'm not sure if this solves my personal use case. For instance, would you be able to include this with FormControl/FormGroup, without having to convert the Date() object to a string first? Thank you

@Qocotzxin
Copy link

@Qocotzxin Qocotzxin commented May 21, 2019

No, of course it will not solve the issue since it's only a workaround, and even more, this way you only get the value exactly as you see it on screen. So, if you look for example something more like a JS date string you would have to use toString() method from Moment.

As far as I know the best (?) way to get this solved is through the adapter. I was finally able to do it by creating a custom adapter extending moment one, but the parse method would only allow as return value 'null' or 'Moment' so we are still missing the string you mention. I'm guessing that just adding a new return type will be the solution for the string issue, and should be the more elegant way to avoid extra logic since the adapter is the only 'place' where you always have to pass if you use the datepicker component. I totally support that, it would be a great feature to avoid unncessary extra parsing before sending a request and you even could choose what kind of string you wanna get since the adapter allows to apply different logics.

@jasonhjohnson
Copy link

@jasonhjohnson jasonhjohnson commented Aug 4, 2021

Hi, any updates on this? Is there a way to return a string without doing extra formatting before sending to backend services?

@BenLune
Copy link

@BenLune BenLune commented Feb 7, 2022

Hi,
Just spent a few hours to check how to return a string into my form rather than a full object (Luxon DateTime in my case). Even with a custom Adapter it seems to not be possible... So I have two possibilities, working at FormControl level by adding a second form control (plugged into my form), listening to datePicker change, then applying the string version of the date picker value. Or listening to my form valueChanges, and then, look at DateTime objects in the form and transform them into string before sending to back-office.
I wonder why so much complexity for such a (seems to be) simple problem.
Maybe are there explanations ?

@CharlieGreenman
Copy link
Author

@CharlieGreenman CharlieGreenman commented Feb 7, 2022

Hey @BenLune feel free to join the github.com/razroo discord and we can chat about it https://discord.gg/KrujUmMC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants