2010-08-04 27 views
1

unix時間戳應該只有十個字符,但是我總是收到13個字符,這導致了我的問題。我怎樣才能解決這個問題?如何在jQuery Datepicker中使用POSIX Timestamp格式來使用秒?

<script type="text/javascript"> 
$(document).ready(function() 
{ 
    $("#startdate").datepicker({ altField: "#startdatehidden", altFormat:$.datepicker.TIMESTAMP, dateFormat:$.datepicker.TIMESTAMP, mandatory: true }); 
}); 
</script> 

回答

2

雖然我會建議您驗證日期服務器並進行修改如果可能的話,例如用戶可能沒有javascript,並且手動將日期放在那裏,無論如何您都應該嘗試將日期轉換爲日期(使用strtotime的php),並且您可以使用相同的塊將js時間戳轉換爲unix時間戳!您需要做的很多事情才能在客戶端做出這樣的防彈,因爲我會解釋一種情況,但回答您的問題並按照您的方式進行操作,這是一種開始的方式:

您需要創建一個單獨的輸入字段顯示jQuery UI的日期選擇器可以稱之爲

<input id='display_startdate' type='text' name='does-not-matter-server-will-not-use-this'> 

,然後你的舊<input type='hidden' name='startdate' id='startdate'>將被用於服務器端的代碼,但你不會在這一領域應用的jQuery日期選擇器。你可以把它作爲類型文本進行測試,然後將其更改爲隱藏,因爲用戶不需要看到這個!

$(function(){ 
     $("#display_startdate").datepicker({ 
       altFormat: '@' , 
       altField: '#Inspection_End', 
       //now the lines below will divide the ms by 1000 to give you POSIX TIMESTAMP 
       onSelect: function() { 
        fixTime(); 
       } 
     }); 
    }); 
    //There are other times you need to evaluate the value of this field 
    //as an example some browsers will keep the input user entered on page refresh 
    //but will not keep the value javascript created 
    $('#yourForm').submit(function(){ 
    //or $('#yourSubmit').click(function(){ 
      fixTime(); 
      //do rest! 
    }); 
    function fixTime(){ 
     $('#startdate').val($('#startdate').val()/1000); 
    } 
0

你的意思是你要像1280943953 代替1280943953?

如果是的話,那是因爲JavaScript以毫秒爲單位計算時間,所以你應該儘量
鴻溝時間戳1000

子串的最後三位數字

+0

我試圖從jquery.ui.datepicker.js中除以1000,但我不知道該在哪裏做。我試過的兩個地方都導致時間戳顯示負數。 – dallen 2010-08-04 18:48:02

+0

嗯......你有最新版本的插件嗎?看來這個問題已經在4.0.0版本中得到了糾正 – aletzo 2010-08-04 21:15:02

相關問題