0

i have a submit form where has a input field for group name. I want to create a group name and stote in the database. when the group name create thats time i want to pick the current date and store in the model class propert. please give me suggestion. Advance thank you.

<input type="hidden" asp-for="DateTime.Date" class="form-control" />
<input asp-for="Name" type="text" class="form-control" id="inputName" 
placeholder="Group name">

 <script>
    $(function () {
        //Date picker
        $('#reservationdate').datetimepicker({
            format: 'L',
            format: 'dd/mm/yyyy'
        });
    }
</script>

model property public DateTime DateTime { get; set; } public string Name { get; set; }

Mithun CH
  • 1
  • 4
  • Are you looking for getting current date and time in js? Check this out: https://stackoverflow.com/questions/10211145/getting-current-date-and-time-in-javascript – nfn Sep 12 '21 at 11:07

2 Answers2

1

don't get the date from client side get it form the server side

use DateTime in c# to get the current date and store it

var date = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); or use any date format you like

AL.Sharie
  • 1,526
  • 1
  • 9
  • 14
  • i do not want to write hradcore , i want when i create a group that's time pike the current date, i have not option to write manually because this property data will be pass in the business object using mapper, like this var group = _mapper.Map(this); _groupService.CreateGroup(group); – Mithun CH Sep 12 '21 at 11:32
  • This is not a solution to the problem. Not sure why this has got a upvote! – Rahatur Sep 12 '21 at 16:12
0

Solution-1:

If you are using SQL server then directly use GETDATE() method in your insert and update queries.

Solution-2:

In your HTTP GET method when the page is loaded initialize the property as follow i.e.

    // Initialization.
    MyModel model = new MyModel();
    
    // Settings.
    model.DateTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");

Then in your CSHTML file do following i.e.

<div class="form-group">
     @Html.HiddenFor(m => m.DateTime, new { @readonly = "readonly", placeholder = Html.DisplayNameFor(m => m.DateTime), @class = "form-control" })
</div>

The above HTML hidden field will preserve the datetime value, but, know that the time might not be accurate as it is not exact current, so, I recommend setting current date via database is right choice.

asmak9
  • 239
  • 2
  • 7