博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

js将时间戳转换成正常的yyyy-m-d格式

Posted on 2013-10-23 17:23  人生梦想起飞  阅读(759)  评论(0编辑  收藏  举报

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>无标题文档</title>

</head>

<body>

</body>

</html>

<script type="text/javascript">

function for_php_date_to_str(timestamp)

{

    update=new Date(timestamp*1000);//时间戳要乘1000

    year=update.getFullYear();

    month=(update.getMonth()+1<10)?('0'+(update.getMonth()+1)):(update.getMonth()+1);

    day=(update.getDate()<10)?('0'+update.getDate()):(update.getDate());

 

    hour=(update.getHours()<10)?('0'+update.getHours()):(update.getHours());

    minute=(update.getMinutes()<10)?('0'+update.getMinutes()):(update.getMinutes());

    second=(update.getSeconds()<10)?('0'+update.getSeconds()):(update.getSeconds());

 

    return (year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second);

}

alert(for_php_date_to_str("1381313200"))

</script>