0

I have 2 a elements. Both open a Bootstrap modal. This a element is used to create a brand new record, and so it sends the current date/time:

<a style="color:#5cb85c" data-CreateDate="@DateTime.Now" data-toggle="modal" data-target="#OnPostModal" href="#OnPostModal" class="fas fa-plus-circle"></a>

The other a element carries some fields from a record that was already created, so it sends that pre-existing create date with it.

<a data-toggle="modal" data-target="#OnPostModal" data-CreateDate="@item.Supplier.CreateDate" href="#CallModal" class="fas fa-edit"> </a>

I use some JavaScript to fill a field on the modal that pops up:

<script type="text/javascript">
    $('a[data-toggle=modal], button[data-toggle=modal]').click(function () {

        var data_CreateDate = '';

        if (typeof $(this).data('CreateDate') !== 'undefined') {
            data_CreateDate = $(this).data('CreateDate');
        }

        $('#CreateDate').val(data_CreateDate);
    })

</script>

Lastly, this is the area in the modal that needs filled:

<div class="form-group">
   <label asp-for="Supplier.CreateDate" class="control-label"></label>
   <input asp-for="Supplier.CreateDate" id="CreateDate" class="form-control" />
   <span asp-validation-for="Supplier.CreateDate" class="text-danger"></span>
</div>

This method works for string values, but doesn't seem to work at all for DateTime or Date values. I have tried:

var data_CreateDate = new Date();

That doesn't work.

CusterN
  • 93
  • 8
  • Does this answer your question? [How to set datetime on datetime-local via jQuery](https://stackoverflow.com/questions/15484772/how-to-set-datetime-on-datetime-local-via-jquery) – Heretic Monkey Apr 06 '20 at 16:23
  • Check the format. – Heretic Monkey Apr 06 '20 at 16:24
  • @HereticMonkey If I change the modal input to no validation, the string value of "4/6/2020 2:01:52 PM" but as soon as I add the `asp-for="Supplier.CreateDate"` the date is no longer passed. I think it's as you said, the format needs tweaked to match the asp-for validation. – CusterN Apr 06 '20 at 18:21
  • `input type="datetime"` has been deprecated in favor of `input type="datetime-local"`. The format required for setting the value of such an input is described in the answers to the question I linked to... – Heretic Monkey Apr 06 '20 at 18:32
  • @HereticMonkey the documentation on tag helpers https://learn.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-3.1#the-input-tag-helper states that because my Supplier.CreateDate is a DateTime, the implementation adds the type="datetime-local" so I don't think that's the issue. Though, the moment I add that, the date stops being passed. – CusterN Apr 06 '20 at 20:30
  • I'm basing my comments on the code shown in the question, since that's all I have to go on. If you'd like to show the rendered HTML, that would probably be more helpful. In any case, it's up to you to figure out what format the value of `data_CreateDate` is in, and get it into the format necessary for the type of input you are trying to set, which again, is noted in the answers to that question. – Heretic Monkey Apr 06 '20 at 20:35
  • I see! Thank you. I have removed it. My research into the input tag helper identified it as redundant. – CusterN Apr 06 '20 at 20:52

1 Answers1

1

new Date() returns an object. You want a string. Several formats to choose from:

const output = document.getElementById('output');
const d = new Date();
output.innerText = d.toString() + '\n';
output.innerText += d.toISOString() + '\n';
output.innerText += d.toGMTString() + '\n';
output.innerText += d.toLocaleDateString() + '\n';
output.innerText += d.toUTCString() + '\n';
<pre id="output"></pre>
terrymorse
  • 6,771
  • 1
  • 21
  • 27