时间戳转为时间格式

来源:互联网 发布:粒子群算法的gui 编辑:程序博客网 时间:2024/05/17 08:20

**
 * 时间戳转时间格式
 * @param
  jsondate 得到的number 型时间数
 */

  function getLocalTime(jsondate) { 
jsondate=""+jsondate+"";//因为jsonDate是number型的indexOf会报错
if (jsondate.indexOf("+") > 0) {
                jsondate = jsondate.substring(0, jsondate.indexOf("+"));
            }
            else if (jsondate.indexOf("-") > 0) {
                jsondate = jsondate.substring(0, jsondate.indexOf("-"));
            }
            var date = new Date(parseInt(jsondate, 10));
            var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
            var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
            var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
            var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
            var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
            return date.getFullYear() + "-" + month + "-" + currentDate + " " + hours + ":" + minutes + ":" + seconds;
}   

问题解决(ps:有一点要注意的是得到的可能是number型的,在js中不能使用indexOf方法,需要加引号转为字符创格式)

原创粉丝点击