2012-04-24 86 views
4

我有jQuery的2個datepickers,我一天增加一個選擇器的日期和它分配到其他日期選擇器,像這樣:jQuery的日期選擇問題格式化日期

/*assign date pickers*/ 
     $("#MainContent_txtActualShipDate").datepicker({ 
      dateFormat: 'mm/dd-yyyy', 
      changeMonth: true, 
      changeYear: true, 
      showOtherMonths: true, 
      selectOtherMonths: true 
     }); 

     $("#MainContent_txtExpectedShipDate").datepicker({ 
      changeMonth: true, 
      changeYear: true, 
      showOtherMonths: true, 
      selectOtherMonths: true, 
      onSelect: function (selectedDate) { 
       var date = $(this).datepicker('getDate'); 

       if (date) { 
        date.setDate(date.getDate() + 1); 
        $("#MainContent_txtActualShipDate").val(date); 
       } 

      } 
     }); 

通知的onSelect功能增加了一個單日期並將其分配給#MainContent_txtActualShipDate,但它獲得的值爲「TUE APR 24 2012 00:00:00」。 我希望它只是'04/24/2012'如此mm/dd/yyyy。我閱讀了jquery ui文檔,並在stackoverflow上查找了一些示例,但沒有一個能夠工作。有沒有人可以查看此代碼並進行編輯以獲取我想要的格式?

編輯

這是我將其更改爲:

 $("#MainContent_txtExpectedShipDate").datepicker({ 
      dateFormat: 'mm/dd-yyyy', 
      changeMonth: true, 
      changeYear: true, 
      showOtherMonths: true, 
      selectOtherMonths: true, 
      onSelect: function (selectedDate) { 
       var date = $(this).datepicker('getDate'); 

       if (date) { 
        date.setDate(date.getDate() + 1); 
        var formattedDate = $.datepicker.formatDate('mm/dd-yyyy', date); 
        $("#MainContent_txtActualShipDate").val(formattedDate); 

//     $("#MainContent_txtActualShipDate").val(date); 
       } 

      } 
     }); 

但它顯示了每年兩次04/12/20122012。

回答

下面是其他人在未來什麼工作:

/*assign date pickers*/ 
     $("#MainContent_txtActualShipDate").datepicker({ 
      dateFormat: 'mm/dd/yy', 
      changeMonth: true, 
      changeYear: true, 
      showOtherMonths: true, 
      selectOtherMonths: true 
     }); 

     $("#MainContent_txtExpectedShipDate").datepicker({ 
      dateFormat: 'mm/dd/yy', 
      changeMonth: true, 
      changeYear: true, 
      showOtherMonths: true, 
      selectOtherMonths: true, 
      onSelect: function (selectedDate) { 
       var date = $(this).datepicker('getDate'); 

       if (date) { 
        date.setDate(date.getDate() + 1); 
        var formattedDate = $.datepicker.formatDate('mm/dd/yy', date); 
        $("#MainContent_txtActualShipDate").val(formattedDate); 
       } 

      } 
     }); 
     /*end assign date pickers*/ 

感謝

+0

知道了我的格式有誤我需要'mm/dd/yy' – oJM86o 2012-04-24 13:02:29

回答

4

使用formatDate()功能:

var formattedDate = $.datepicker.formatDate('mm/dd/yy', date); 
$("#MainContent_txtActualShipDate").val(formattedDate); 
+0

由於某種原因,它顯示了一年兩次...結果是2012年4月26日? – oJM86o 2012-04-24 12:58:33

+0

您的日期格式正確,但結果如下所示:2012年4月26日?在編輯中查看我的代碼。 – oJM86o 2012-04-24 12:59:35

+0

明白了我的格式有誤我需要'mm/dd/yy'。 – oJM86o 2012-04-24 13:02:58

0

嘗試設置的日期像

$('#MainContent_txtActualShipDate').datepicker("setDate", date);

如果這不起作用,

$("#MainContent_txtActualShipDate").val(date.getMonth()+"/"+date.getDate()+"/"+date.getFullYear());

0

你錯過了日期格式試試這個:

/*assign date pickers*/ 
    $("#MainContent_txtActualShipDate").datepicker({ 
     dateFormat: 'mm/dd/yy', 
     changeMonth: true, 
     changeYear: true, 
     showOtherMonths: true, 
     selectOtherMonths: true 
    }); 

    $("#MainContent_txtExpectedShipDate").datepicker({ 
     dateFormat: 'mm/dd/yy', 
     changeMonth: true, 
     changeYear: true, 
     showOtherMonths: true, 
     selectOtherMonths: true, 
     onSelect: function (selectedDate) { 
      var date = $(this).datepicker('getDate'); 

      if (date) { 
       date.setDate(date.getDate() + 1); 
       var formattedDate = $.datepicker.formatDate('mm/dd/yy', date); 
       $("#MainContent_txtActualShipDate").val(formattedDate); 
      } 

     } 
    }); 
+0

這不工作它仍然分配長字符串txtActualShipDate – oJM86o 2012-04-24 12:54:48

+1

你有添加YYYY減少到YY – 2012-04-24 13:12:30

0

你有錯誤的日期格式,它必須是「DD/MM/yy'

我也傾向於只將類添加到任何需要datepicker的字段。因此,你可以做這樣的事情...

$(".DatePick").datepicker({ dateFormat: 'dd/mm/yy', gotoCurrent: true }); 

保存必須分配多次。