Js将unix时间戳转化成年月日时分秒格式

<div id="unixToYmd">
            <p>
                Unix时间戳:<input style="width:120px;height:18px;" type="text" name="timeStamp" id="timeStamp" />
                <input type="button" name="switch" id="switch" name="switch" value="转换" />
                时间日期:<input style="width:250px;height:18px;" type="text" name="ymd" id="ymd" />
            </p>  
 </div>
 <script language="javascript">
  jQuery(document).ready(function(){
            $('#switch').click(function(){
                var timeStamp=$('#timeStamp').val();
                reg=/^\d+$/;
                if((timeStamp!='') && (reg.test(timeStamp))) {
                    var ymd=new Date();
                    ymd.setTime(timeStamp*1000);
                    var year=ymd.getFullYear().toString();
                    var month=(ymd.getMonth()+1).toString();
                    var date=ymd.getDate().toString();
                    var hour=ymd.getHours().toString();
                    var minute=ymd.getMinutes().toString();
                    var second=ymd.getSeconds().toString();
                    $('#ymd').val(year+'-'+month+'-'+date+' '+hour+':'+minute+':'+second);
                }
                else {
                    alert('请输入unix时间戳(秒)');
                }
            });   
  });
 </script>

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注