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

前端 未结 3 1054
半阙折子戏
半阙折子戏 2020-12-18 05:34

I\'m using jQuery\'s datepicker plugin in .NET ASP MVC3 intranet application. User that use application have offices in different countries and different locale. This is why

相关标签:
3条回答
  • 2020-12-18 05:45

    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

    0 讨论(0)
  • 2020-12-18 05:51

    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>
    
    0 讨论(0)
  • 2020-12-18 06:03

    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.

    0 讨论(0)
提交回复
热议问题