2010-08-04 24 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

相关问题