2

I have an input field in my project that uses the HTML5 date input type. This is saved to my database in the format YYYY-MM-DD. In another page, I access this date from a Twig and print it in a table. This prints in the format YYYY-MM-DD. So far so good. However, when I try to use an alert box to print the same date, JavaScript parses the date incorrectly to just show the year (and the wrong year at that).

Input field (as part of a bigger "make plan" input form):

<input type="date" id="start" name="startdate" class="form-control" required="required"/>

Output:

<td>{{plan.startdate}}</td> <!-- This prints correctly YYYY-MM-DD -->

<td>
    <script>
        alert({{plan.startdate}});   <!-- This alerts incorrectly YYYY -->                    
    </script>
</td>

I can't really see the correlation between the dates either. 2014-03-27 gives '1984' in the alert box 2014-03-20 gives '1991' in the alert box 2014-04-01 gives '2009' in the alert box

I've tried to parse the date using the JavaScript, but I'm not really sure how to parse an incorrect year.

CameronD17
  • 177
  • 6

1 Answers1

1

It is being evaluated as an expression instead of a date string. If you put the date in quotation marks then it should be fine. See the following

<td>{{plan.startdate}}</td> <!-- This prints correctly YYYY-MM-DD -->

<td>
    <script>
        alert("{{plan.startdate}}");   <!-- This alerts incorrectly YYYY -->                    
    </script>
</td>
Smittey
  • 2,475
  • 10
  • 28
  • 35