【问题标题】:Current day , month, year from datepicker日期选择器中的当前日、月、年
【发布时间】:2016-12-22 06:13:23
【问题描述】:

我有下面的代码可以让我知道当前日期。

$('#txtSelectedDate').datepicker({
    showButtonPanel: true,
    currentText: "Today:" + $.datepicker.formatDate('dd mm yy', new Date())
});  

我如何分别提取到alert日、月、年。

【问题讨论】:

  • 你能用语法告诉我吗!?,JS 和 datepicker 对我来说是个新手。谢谢
  • 您使用的是哪个日期选择器?
  • 我觉得你应该做var date = $.datepicker.formatDate('dd mm yy', new Date()); alert("Day:" + data.getDay() ...);这样的事情但是我没有检查这些方法是否可用
  • ''是我利用的

标签: javascript html datepicker


【解决方案1】:
var d = new Date();

var date = d.getDate();

var month=d.getMonth()+1;
// we are adding 1 to getMonth Method, becoz it will return 0 to 11

var year=d.getFullYear();

希望对您有所帮助..

【讨论】:

    【解决方案2】:

    请尝试,

    var today = new Date();
    console.log(today.getDate());
    console.log(today.getMonth());
    console.log(today.getFullYear());
    

    SoF 参考 How to format a JavaScript date

    【讨论】:

      【解决方案3】:

      所以在网上查看后,您可以这样做:

      var date = $.datepicker.formatDate('dd mm yy', new Date());
      alert("Day:" + date.getDay() + "month:" + date.getMonth() + "year:" + date.getFullYear());
      

      【讨论】:

        【解决方案4】:

        试试这个。 您可以使用onSelect 事件

        <html>
        
        <head>
            
            <link href="http://code.jquery.com/ui/1.9.2/themes/smoothness/jquery-ui.css" rel="stylesheet" />
            <input type='text' class='date'>
            <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
            <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
            <script>
                $(function ($) {
        
                    $("#dataPicker").datepicker({
                        onSelect: function (dateText) {
                            display("Selected date: " + dateText + "; input's current value: " + this.value);
                            alert("Day " + (new Date(dateText)).getDate() + " Month: " + (new Date(dateText)).getMonth() + " Year: " +  (new Date(dateText)).getFullYear())
                        }
                    }).on("change", function () {
                        display("Got change event from field");
                    });
        
                    function display(msg) {
                        $("<p>").html(msg).appendTo(document.body);
                    }
        
                });
            </script>
        </head>
        
        <body>
            <div id="dataPicker">
        
            </div>
        </body>
        
        </html>

        【讨论】:

          【解决方案5】:

          我认为你应该这样做

           $('#txtSelectedDate').datepicker({
                  showButtonPanel: true,
                  currentText: "Today:" + $.datepicker.formatDate('dd mm yy', new Date()),
                 onSelect: function(){
                          var day = $("#txtSelectedDate").datepicker('getDate').getDate(); 
              alert(day);                
                          var month = $("#txtSelectedDate").datepicker('getDate').getMonth() + 1;  
              alert(month);           
                          var year = $("#txtSelectedDate").datepicker('getDate').getFullYear();
                         alert(year);
                      }
              });  
          

          【讨论】:

          • 感谢 Rajesh,但我正在尝试获取今天的日期信息。您的建议将提供从 datepicker 中选择的日期
          • @shwink 你想提醒今天的日、月、年还是打印?
          • 是的,没错。我需要检查所选日期不应早于今天。
          【解决方案6】:

          使用指定的 altField 选项初始化日期选择器:

          $( ".selector" ).datepicker({
             altField: "#actualDate"
          });
          

          使用指定的 altFormat 选项初始化日期选择器:

          $( ".selector" ).datepicker({
             altFormat: "yy-mm-dd"
          });
          

          详情请参考:-

          http://api.jqueryui.com/datepicker/

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-07
            • 2013-10-26
            • 1970-01-01
            相关资源
            最近更新 更多