1

I have a modal which I use to edit some items. So I'm dynamically pre-filling the input fields using Javascript/JQuery. One of these items is a date.

The field on the form is showing '2016-07-01 00:00:00', which is the value coming from the database directly. This comes from

$(function() {
    $('#edit-auction-modal').on("show.bs.modal", function (e) {
       $('#edit-auction-modal ').find('input#auction-startdate').val($(e.relatedTarget).data('startdate'));

       //$(e.relatedTarget).data('startdate') contains 2016-07-01 00:00:00

When I use "type=text"

<input type="text" class="form-control" id="startdate" placeholder="Start date" name="startdate" />

it displays the field as 2016-07-01 00:00:00 which is the field as it is stored in the database.

How can I change my Javascript code so that it displays 01-07-2016 in that field?

I tried changing the date format in the Javascript file via the following code:

 var mydate = new Date($(e.relatedTarget).data('startdate'));
 var dd = mydate.getDate()
 var mm = mydate.getMonth()+1;
 var yyyy = mydate.getFullYear();
 if(dd<10){
    dd='0'+dd
 }
 if(mm<10){
    mm='0'+mm
 }

  var formattedStartDate = yyyy + '-' + mm + '-' + dd
  var d = new Date(formattedStartDate);

  $('#edit-auction-modal ').find('input#auction-startdate').val(d);

  //printing to log the formattedStartDate gives 2016-07-01

However, in the input field however I'm getting Fri Jul 01 2016 00:00:00 GMT+0200 (CEST) now. How can I apply the format dd-mm-yyyy while still using a date?

wiwa1978
  • 2,317
  • 3
  • 31
  • 67
  • 1
    What's the issue in setting value as `formattedStartDate`? – Mohit Bhardwaj Jul 07 '16 at 15:44
  • Yeah, just make `formattedStartDate` contain the date as you want it formatted, then call `val(formattedStartDate)`. – Heretic Monkey Jul 07 '16 at 15:47
  • Possible duplicate of [Where can I find documentation on formatting a date in JavaScript?](http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – Heretic Monkey Jul 07 '16 at 15:50
  • @MikeMcCaughan When I do that, it displays correctly in the input field, but when I then change it, the date saved in the database is 0000-00-00 00:00:00 and this disaplys as 30-11--0001 in the input field. – wiwa1978 Jul 07 '16 at 15:53
  • There's obviously more going on with this than what you're showing. Please read how to create a [mcve]. – Heretic Monkey Jul 07 '16 at 15:54

0 Answers0