jQuery datepicker's dateFormat - how to integrate with .NET current culture DateTimeFormat

流过昼夜 提交于 2019-11-29 07:25:44

The codeproject article JQueryUI Datepicker in ASP.NET MVC http://www.codeproject.com/Articles/62031/JQueryUI-Datepicker-in-ASP-NET-MVC has function that does exactly what you wanted

    /// Converts the .net supported date format current culture 
/// format into JQuery Datepicker format.
/// </summary>
/// <param name="html">HtmlHelper object.</param>
/// <param name="format">Date format supported by .NET.</param>
/// <returns>Format string that supported in JQuery Datepicker.</returns>
public static string ConvertDateFormat(this HtmlHelper html, string format)

I've also posted a function that does the opposite-Translate jQuery UI Datepicker format to .Net Date format

I've sumbled upon the same problem some time ago. The path I took was just to convert whatever jQuery datepicker provide me to milis (.getTime()). Knowing that javascript time is based on date of 1,1,1970 and .NET on 1,1,0 I'm able to do the calculation on my controller side

So assuming you're passing javscript DateTime.getTime() value to your controller you can ;

var myDate = new DateTime(1970, 1, 1) + new TimeSpan(time * 10000);

in your view you could ;

    $.datepicker.setDefaults($.datepicker.regional["pl"]);

    $("#StartDate").datepicker({
        dateFormat: "yy-mm-dd",
        onSelect: function (dateText) {
            var currentDate = new Date(dateText);
            time = currentDate.getTime();
            // $.post | $.ajax here - whatever you need
        }
    });

You will want to remember about TimeZones and the fact that javascript takes that into account when calculating dates.

store in hidden field

        <input id="dateFormate" type="hidden" 
value='@System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern.ToLower().Replace("yyyy", "yy")'/>

        @Html.HiddenFor(model=>model.StartDate)
        @Html.HiddenFor(model=>model.EndDate)
        <input type="text" id="tbStartDate" value="" disabled="disabled" />
        <input type="text" id="tbEndDate" value="" disabled="disabled" />
        <script type="text/javascript">
            $(document).ready(function () {
                $("#tbStartDate").datepicker({
                    dateFormat: $('#dateFormate').val(),
                    showOn: 'button',
                    buttonImageOnly: true,
                    buttonImage: '/Content/Calendar.png',
                    buttonText: 'Click here (date)',
                    onSelect: function (dateText, inst) {
                        var $endDate = $('#tbStartDate').datepicker('getDate');
                        $endDate.setDate($endDate.getDate() + 1);
                        $('#tbEndDate').datepicker('setDate', $endDate).datepicker("option", 'minDate', $endDate);
                    },
                    onClose: function (dateText, inst) {
                        $("#StartDate").val($("#tbStartDate").val());
                    }
                });

                $("#tbEndDate").datepicker({
                    dateFormat: $('#df').val(),
                    showOn: 'button',
                    buttonImageOnly: true,
                    buttonImage: '/Content/Calendar.png',
                    buttonText: 'Click here (date)',
                    onClose: function (dateText, inst) {
                        $("#EndDate").val($("#tbEndDate").val());
                    }
                });

                var $endDate = $('#tbStartDate').datepicker('getDate');
                $endDate.setDate($endDate.getDate() + 1);
                $('#tbEndDate').datepicker('setDate', $endDate).datepicker("option", 'minDate', $endDate);
            });
        </script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!