0

Hello all I am trying to set the datepicker options minDate, maxDate, and defaultDate from materializecss in javascript. I am using version 1.0.0-rc.1 build. In the documentation it states that the type for these options are Date objects. When I open the picker, the options are not set and in fact (see chrome dev tools screenshot) the options are null or invalid. Any ideas? Thanks

Code:

<script>
var startDateElem = document.getElementById('student_sdate');
var startDateInstance = M.Datepicker.init(startDateElem, {minDate: new Date("<?php echo $projectTuple['sdate']; ?>"),
                                      maxDate: new Date("<?php echo $projectTuple['edate']; ?>"),
                                      defaultDate: new Date("<?php echo $projectTuple['sdate']; ?>"),
                                      onSelect: function(newDate){
                                        endDateInstance.options.minDate = newDate;
                                      }});
var endDateElem = document.getElementById('student_edate');
var endDateInstance = M.Datepicker.init(endDateElem, {minDate: new Date("<?php echo $projectTuple['sdate']; ?>"),
                                      maxDate: new Date("<?php echo $projectTuple['edate']; ?>"),
                                      defaultDate: new Date("<?php echo $projectTuple['edate']; ?>"),
                                      onSelect: function(newDate){
                                        startDateInstance.options.maxDate = newDate;
                                      }});
</script>
Austin H
  • 1
  • 1
  • What is the value returned by `""` and similar calls? Using the built-in parser for anything other than the one format supported by [*ECMA-262*](http://ecma-international.org/ecma-262/8.0/#sec-date-time-string-format) is not recommended. – RobG May 05 '18 at 09:30
  • @RobG the value is 2009-08-29 – Austin H May 05 '18 at 19:38
  • "2009-08-29" will be parsed as UTC (which is inconsistent with ISO 8601), so if you are west of Greenwich it will show the local date as 2009-08-28, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG May 06 '18 at 03:24
  • Thank you I changed the format and it seems to be working properly. That post was is really helpful – Austin H May 07 '18 at 04:46

1 Answers1

0

I found a solution that works for me. I was unable to set the options at initialization so I used setTimeout in JavaScript to set the options then. This works for my situation as this project is for school.

Austin H
  • 1
  • 1