简体   繁体   中英

Change Datepicker Date Format for Sql

https://jsfiddle.net/jquerybyexample/dxbtp/494/

i used this code for Date Range but this input format is MM/dd/yyyy . i want change this format MM/dd/yyyy for send my Database.

if i change this line

        var dtFormatted = mm + '/'+ dd + '/'+ y;

to

        var dtFormatted = dd+ '/'+ mm + '/'+ y;

input tag doesn't work properly

Here is a working demo:

<div>
    <input type="text" id="txtFromDate" />To:
    <input type="text" id="txtToDate" />
</div>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" />
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
    <script>
        $('input[id$=txtFromDate]').datepicker({
            dateFormat: 'dd/mm/yy'
        });
        $('input[id$=txtToDate]').datepicker({
            dateFormat: 'dd/mm/yy'
        });
    </script>

result: 在此处输入图像描述

dtFormatted设置为字符串,格式如下。

var dtFormatted = "dd/mm/yy";

I fixed

 <input asp-for="@Model.Reservation.StartDate" type="text" class="datepicker" id="txtFromDate" autocomplete="off" required placeholder="Check In">

<input asp-for="@Model.Reservation.EndDate" type="text" class="datepicker" autocomplete="off" id="txtToDate" required placeholder="Check Out">

<script type="text/javascript">
    
    $(document).ready(function () {
        $('#txtToDate').datepicker({
            dateFormat: "dd/mm/yy"
        });
        $("#txtFromDate").datepicker({
            dateFormat: "dd/mm/yy",
            minDate: new Date(),
            onSelect: function (date) {
                var date1 = $('#txtFromDate').datepicker('getDate');
                var date = new Date(Date.parse(date1));
                date.setDate(date.getDate() + 1);
                var newDate = date.toDateString();
                newDate = new Date(Date.parse(newDate));
                $('#txtToDate').datepicker("option", "minDate", newDate);
            }
        });
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM