0

i know it's a question asked many times here, but i tried all the solutions and no one is working. I want to change the output in my CRUD from the input form to dd/mm/yyyy and not in yyyy-mm-dd.

Here's my code:

var nextId = 1;
var activeId = 0;
function productDisplay(ctl) {
  var row = $(ctl).parents("tr");
  var cols = row.children("td");
  activeId = $($(cols[0]).children("button")[0]).data("id");
  $("#productname").val($(cols[1]).text());
  $("#introdate").val($(cols[2]).text());
  $("#finishdate").val($(cols[3]).text());
  $("#url").val($(cols[4]).text());
  $("#phone").val($(cols[5]).text());
  $("#note").val($(cols[6]).text());
  $("#client").val($(cols[7]).text());
  $("#updateButton").text("Aggiorna");
}

function productUpdate() {
  if ($("#updateButton").text() == "Add") {
    productUpdateInTable(activeId);
  }
  else {
    productAddToTable();
  }
  formClear();
  $("#productname").focus();
}

function productAddToTable() {
  if ($("#productTable tbody").length == 0) {
    $("#productTable").append("<tbody></tbody>");
  }

  $("#productTable tbody").append(
    productBuildTableRow(nextId));
  nextId += 1;
}
function productUpdateInTable(id) {
  var row = $("#productTable button[data-id='" + id + "']")
    .parents("tr")[0];

  $(row).after(productBuildTableRow(id));
  $(row).remove();

  formClear();
  $("#updateButton").text("Add");
}
function productBuildTableRow(id) {
  var ret =
    "<tr>" +
    "<td>" +
    "<button type='button' " +
    "onclick='productDisplay(this);' " +
    "class='btn btn-default' " +
    "data-id='" + id + "'>" +
    "<span class='glyphicon glyphicon-pencil' />" +
    "</button>" +
    "</td>" +
    "<td>" + $("#productname").val() + "</td>" +
    "<td>" + $("#introdate").val() + "</td>" +
    "<td>" + $("#finishdate").val() + "</td>" +
    "<td>" + $("#url").val() + "</td>" +
    "<td>" + $("#phone").val() + "</td>" +
    "<td>" + $("#note").val() + "</td>" +
    "<td>" + $("#client").val() + "</td>" +
    "<td>" +
    "<button type='button' " +
    "onclick='productDelete(this);' " +
    "class='btn btn-default' " +
    "data-id='" + id + "'>" +
    "<span class='glyphicon glyphicon-minus' />" +
    "</button>" +
    "</td>" +
    "</tr>"
  return ret;
}

function productDelete(ctl) {
  var result = confirm("Want to delete record?");
  if (result) {
    var result2 = confirm("Really?");
  }
  if (result2) {
    $(ctl).parents("tr").remove();
  }
}

function formClear() {
  $("#productname").val("");
  $("#introdate").val("");
  $("#finishdate").val("");
  $("#url").val("");
  $("#phone").val("");
  $("#note").val("");
  $("#client").val("");
}
function doSearch(text, color = "yellow") {
  if (color != "transparent") {
    doSearch(document.getElementById('hid_search').value, "transparent");
    document.getElementById('hid_search').value = text;
  }
  if (window.find && window.getSelection) {
    document.designMode = "on";
    var sel = window.getSelection();
    sel.collapse(document.body, 0);

    while (window.find(text)) {
      document.execCommand("HiliteColor", false, color);
      sel.collapseToEnd();
    }
    document.designMode = "off";
  } else if (document.body.createTextRange) {
    var textRange = document.body.createTextRange();
    while (textRange.findText(text)) {
      textRange.execCommand("BackColor", false, color);
      textRange.collapse(false);
    }
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
    <div class="row">
        <div class="col-sm-6">
            <h2><b>Availability</h2></b>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-6">
            <table id="productTable" class="table table-bordered table-condensed table-striped">
                <thead>
                    <tr>
                        <th class="col-sm-0">Modify</th>
                        <th class="col-sm-2">Name</th>
                        <th class="col-sm-1">From</th>
                        <th class="col-sm-1">To</th>
                        <th class="col-sm-2">Area</th>
                        <th class="col-sm-2">Phone</th>
                        <th class="col-sm-3">Note</th>
                        <th class="col-sm-2">Client</th>
                        <th class="col-sm-0">Delete</th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-5">
            <div class="panel panel-primary">
                <div class="panel-heading">
                    Add Availability
                </div>
                <div class="panel-body">
                    <div class="form-group">
                        <label for="productname">
                            Name
                        </label>
                        <input type="text" class="form-control" value="" id="productname" />
                    </div>
                    <div class="form-group">
                        <label for="introdate">
                            From
                        </label>
                        <input type="date" class="form-control" value="gg/mm/aaaa" id="introdate">
                    </div>
                    <div class="form-group">
                        <label for="introdate">
                            To
                        </label>
                        <input type="date" class="form-control" value="gg/mm/aaaa" id="finishdate" />
                    </div>
                    <div class="form-group">
                        <label for="area">
                            Area
                        </label>
                        <input type="search" class="form-control" value="" id="url" />
                    </div>
                    <div class="form-group">
                        <label for="phone">
                            Phone
                        </label>
                        <input type="" class="form-control" value="" id="phone" />
                    </div>
                    <div class="form-group">
                        <label for="note">
                            Note
                        </label>
                        <input type="" class="form-control" value="" id="note" />
                    </div>
                    <div class="form-group">
                        <label for="client">
                            Client
                        </label>
                        <select id="client" class="form-control">
                            <option value=""></option>
                            <option value="1">1</option>
                            <option value="2">2</option>
                            <option value="3">3</option>
                            <option value="4">4</option>
                            <option value="5">5</option>
                            <option value="6">6</option>
                        </select>
                    </div>
                    <div class="panel-footer">
                        <div class="row">
                            <div class="col-xs-12">
                                <button type="button" id="updateButton" class="btn btn-primary" onclick="productUpdate();">
                                    Add
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

When I insert a new record from form field date, the output in my CRUD is like YYYY-MM-DD.

pilchard
  • 12,414
  • 5
  • 11
  • 23
Mr. H
  • 19
  • 4
  • *"i tried all the solutions and no one is working"* And why do you expect a different solution this time? – gre_gor Oct 16 '22 at 12:56
  • Maybe with all the solutions I tried I was wrong with something. I'm not a java script rookie, so I'm trying to find help directly on my code @gre_gor – Mr. H Oct 16 '22 at 13:01
  • @pilchard I tried with toLocaleDateString but maybe I've done some syntax errors cause doesn't work and don't know how to fix it's. Can you help me with an example directly on my code? – Mr. H Oct 16 '22 at 13:04
  • 1
    @Mr.H Please add an input/output example, so I can provide you a function to do that – Mohamed EL-Gendy Oct 16 '22 at 13:06
  • @MohamedEL-Gendy if you run code you can see that. when i add new row with a date, in crud the format is yyyy-mm-dd and i want to put it in dd/mm/yyyy – Mr. H Oct 16 '22 at 13:15
  • @MohamedEL-Gendy thank you so much for the function. But when i add new record and go to modify, all the fields return in form for being modified, exept my date. Is there any way to fix it? – Mr. H Oct 18 '22 at 13:40

1 Answers1

1

This should help you. Pass a valid date in any format and the function will return a dd/mm/yyyy date format

// Helper function to format date from $(input).val()

function formatDate(date) {
  if (date == '') {
    return '';
  }
  let d = new Date(date),
    ye = new Intl.DateTimeFormat('en', {
      year: 'numeric'
    }).format(d),
    mo = new Intl.DateTimeFormat('en', {
      month: '2-digit'
    }).format(d),
    da = new Intl.DateTimeFormat('en', {
      day: '2-digit'
    }).format(d);
  return (`${da}-${mo}-${ye}`);
}

// ----

var nextId = 1;
var activeId = 0;
function productDisplay(ctl) {
  var row = $(ctl).parents("tr");
  var cols = row.children("td");
  activeId = $($(cols[0]).children("button")[0]).data("id");
  $("#productname").val($(cols[1]).text());
  $("#introdate").val($(cols[2]).text());
  $("#finishdate").val($(cols[3]).text());
  $("#url").val($(cols[4]).text());
  $("#phone").val($(cols[5]).text());
  $("#note").val($(cols[6]).text());
  $("#client").val($(cols[7]).text());
  $("#updateButton").text("Aggiorna");
}

function productUpdate() {
  if ($("#updateButton").text() == "Add") {
    productUpdateInTable(activeId);
  }
  else {
    productAddToTable();
  }
  formClear();
  $("#productname").focus();
}

function productAddToTable() {
  if ($("#productTable tbody").length == 0) {
    $("#productTable").append("<tbody></tbody>");
  }

  $("#productTable tbody").append(
    productBuildTableRow(nextId));
  nextId += 1;
}
function productUpdateInTable(id) {
  var row = $("#productTable button[data-id='" + id + "']")
    .parents("tr")[0];

  $(row).after(productBuildTableRow(id));
  $(row).remove();

  formClear();
  $("#updateButton").text("Add");
}
function productBuildTableRow(id) {
  var ret =
    "<tr>" +
    "<td>" +
    "<button type='button' " +
    "onclick='productDisplay(this);' " +
    "class='btn btn-default' " +
    "data-id='" + id + "'>" +
    "<span class='glyphicon glyphicon-pencil' />" +
    "</button>" +
    "</td>" +
    "<td>" + $("#productname").val() + "</td>" +
    "<td>" + formatDate($("#introdate").val()) + "</td>" +    // call helper function
    "<td>" + formatDate($("#finishdate").val()) + "</td>" +   // call helper function
    "<td>" + $("#url").val() + "</td>" +
    "<td>" + $("#phone").val() + "</td>" +
    "<td>" + $("#note").val() + "</td>" +
    "<td>" + $("#client").val() + "</td>" +
    "<td>" +
    "<button type='button' " +
    "onclick='productDelete(this);' " +
    "class='btn btn-default' " +
    "data-id='" + id + "'>" +
    "<span class='glyphicon glyphicon-minus' />" +
    "</button>" +
    "</td>" +
    "</tr>"
  return ret;
}

function productDelete(ctl) {
  var result = confirm("Want to delete record?");
  if (result) {
    var result2 = confirm("Really?");
  }
  if (result2) {
    $(ctl).parents("tr").remove();
  }
}

function formClear() {
  $("#productname").val("");
  $("#introdate").val("");
  $("#finishdate").val("");
  $("#url").val("");
  $("#phone").val("");
  $("#note").val("");
  $("#client").val("");
}
function doSearch(text, color = "yellow") {
  if (color != "transparent") {
    doSearch(document.getElementById('hid_search').value, "transparent");
    document.getElementById('hid_search').value = text;
  }
  if (window.find && window.getSelection) {
    document.designMode = "on";
    var sel = window.getSelection();
    sel.collapse(document.body, 0);

    while (window.find(text)) {
      document.execCommand("HiliteColor", false, color);
      sel.collapseToEnd();
    }
    document.designMode = "off";
  } else if (document.body.createTextRange) {
    var textRange = document.body.createTextRange();
    while (textRange.findText(text)) {
      textRange.execCommand("BackColor", false, color);
      textRange.collapse(false);
    }
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
    <div class="row">
        <div class="col-sm-6">
            <h2><b>Availability</h2></b>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-6">
            <table id="productTable" class="table table-bordered table-condensed table-striped">
                <thead>
                    <tr>
                        <th class="col-sm-0">Modify</th>
                        <th class="col-sm-2">Name</th>
                        <th class="col-sm-1">From</th>
                        <th class="col-sm-1">To</th>
                        <th class="col-sm-2">Area</th>
                        <th class="col-sm-2">Phone</th>
                        <th class="col-sm-3">Note</th>
                        <th class="col-sm-2">Client</th>
                        <th class="col-sm-0">Delete</th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-5">
            <div class="panel panel-primary">
                <div class="panel-heading">
                    Add Availability
                </div>
                <div class="panel-body">
                    <div class="form-group">
                        <label for="productname">
                            Name
                        </label>
                        <input type="text" class="form-control" value="" id="productname" />
                    </div>
                    <div class="form-group">
                        <label for="introdate">
                            From
                        </label>
                        <input type="date" class="form-control" value="gg/mm/aaaa" id="introdate">
                    </div>
                    <div class="form-group">
                        <label for="introdate">
                            To
                        </label>
                        <input type="date" class="form-control" value="gg/mm/aaaa" id="finishdate" />
                    </div>
                    <div class="form-group">
                        <label for="area">
                            Area
                        </label>
                        <input type="search" class="form-control" value="" id="url" />
                    </div>
                    <div class="form-group">
                        <label for="phone">
                            Phone
                        </label>
                        <input type="" class="form-control" value="" id="phone" />
                    </div>
                    <div class="form-group">
                        <label for="note">
                            Note
                        </label>
                        <input type="" class="form-control" value="" id="note" />
                    </div>
                    <div class="form-group">
                        <label for="client">
                            Client
                        </label>
                        <select id="client" class="form-control">
                            <option value=""></option>
                            <option value="1">1</option>
                            <option value="2">2</option>
                            <option value="3">3</option>
                            <option value="4">4</option>
                            <option value="5">5</option>
                            <option value="6">6</option>
                        </select>
                    </div>
                    <div class="panel-footer">
                        <div class="row">
                            <div class="col-xs-12">
                                <button type="button" id="updateButton" class="btn btn-primary" onclick="productUpdate();">
                                    Add
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
pilchard
  • 12,414
  • 5
  • 11
  • 23
Mohamed EL-Gendy
  • 566
  • 4
  • 11