最近项目需要在前端将一个13位的时间戳显示成日期格式,在网上查了很多都不符合要求,只有一个是能满足要求的,在这记录一下,说不定以后还用的着。

13位时间戳改为yyyy-MM-dd HH-mm-ss 格式 
目标时间戳:1516324500000

  //将时间戳改为yyyy-MM-dd HH-mm-ss
function formatDateTime(unix) {
var now = new Date(parseInt(unix) * 1);
now = now.toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
if(now.indexOf("下午") > 0) {
if (now.length == 18) {
var temp1 = now.substring(0, now.indexOf("下午")); //2014/7/6
var temp2 = now.substring(now.indexOf("下午") + 2, now.length); // 5:17:43
var temp3 = temp2.substring(0, 1); // 5
var temp4 = parseInt(temp3); // 5
temp4 = 12 + temp4; // 17
var temp5 = temp4 + temp2.substring(1, temp2.length); // 17:17:43
now = temp1 + temp5; // 2014/7/6 17:17:43
now = now.replace("/", "-"); // 2014-7/6 17:17:43
now = now.replace("/", "-"); // 2014-7-6 17:17:43
}else {
var temp1 = now.substring(0, now.indexOf("下午")); //2014/7/6
var temp2 = now.substring(now.indexOf("下午") + 2, now.length); // 5:17:43
var temp3 = temp2.substring(0, 2); // 5
if (temp3 == 12){
temp3 -= 12;
}
var temp4 = parseInt(temp3); // 5
temp4 = 12 + temp4; // 17
var temp5 = temp4 + temp2.substring(2, temp2.length); // 17:17:43
now = temp1 + temp5; // 2014/7/6 17:17:43
now = now.replace("/", "-"); // 2014-7/6 17:17:43
now = now.replace("/", "-"); // 2014-7-6 17:17:43
}
}else {
var temp1 = now.substring(0,now.indexOf("上午")); //2014/7/6
var temp2 = now.substring(now.indexOf("上午")+2,now.length); // 5:17:43
var temp3 = temp2.substring(0,1); // 5
var index = 1;
var temp4 = parseInt(temp3); // 5
if(temp4 == 0 ) { // 00
temp4 = "0"+temp4;
}else if(temp4 == 1) { // 10 11 12
index = 2;
var tempIndex = temp2.substring(1,2);
if(tempIndex != ":") {
temp4 = temp4 + "" + tempIndex;
}else { // 01
temp4 = "0"+temp4;
}
}else { // 02 03 ... 09
temp4 = "0"+temp4;
}
var temp5 = temp4 + temp2.substring(index,temp2.length); // 07:17:43
now = temp1 + temp5; // 2014/7/6 07:17:43
now = now.replace("/","-"); // 2014-7/6 07:17:43
now = now.replace("/","-"); // 2014-7-6 07:17:43
}
return now;
};
alert(formatDateTime(1516324500000));

最新文章

  1. bzoj3208--记忆化搜索
  2. Collections.shuffle
  3. iOS之触摸及手势
  4. Java的线程安全
  5. 这几天研究DotNetOpenAuth4.2源码发现的坑
  6. 【Unity3D游戏开发】基础知识之Tags和Layers (三二)[转]
  7. NodeJS学习笔记(转载)
  8. The resource could not be loaded because the App Transport
  9. uva 11768
  10. ASP.NET网络编程之-HTTP协议
  11. struts2 中 Session的使用简介
  12. python pdb调试以及sublime3快捷键设置
  13. 使用iframe设置frameset的高度
  14. WordPress数据备份
  15. GPIO的配置过程
  16. OnsenUI 前端框架(三)
  17. Centos安装jdk8
  18. python学习第六讲,python中的数据类型,列表,元祖,字典,之列表使用与介绍
  19. Python基础:数据类型-列表与元组(6)
  20. Supercomputer 解题报告

热门文章

  1. ArchLinux下XFCE的一个问题修复:thunar加载的环境变量不正确
  2. centos7:Kafka集群安装
  3. ElasticSearch入门 —— 集群搭建
  4. Java课堂疑问解答与思考3
  5. SpringBoot配置属性之Server参数
  6. 【监控笔记】【3.1】DML(CDC)、DDL(DDL触发器)跟踪数据更改,数据库审计
  7. 牛客练习赛51 C 勾股定理
  8. 本地代码推送到远程git仓库
  9. golang 状态机
  10. 修改admin中App的名称与表的名称