最近项目需要在前端将一个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));

js 将时间戳转为日期格式的更多相关文章

  1. js 时间戳转为日期格式

    原文:js 时间戳转为日期格式 js 时间戳转为日期格式 什么是Unix时间戳(Unix timestamp): Unix时间戳(Unix timestamp),或称Unix时间(Unix time) ...

  2. js时间戳转为日期格式

    转自:http://wyoojune.blog.163.com/blog/static/57093325201131193650725/ 这个在php+mssql(日期类型为datetime)+aja ...

  3. 微信小程序,时间戳转为日期格式

    //数据转化 function formatNumber(n) { n = n.toString() ] ? n : ' + n } /** * 时间戳转化为年 月 日 时 分 秒 * number: ...

  4. 微信小程序时间戳转为日期格式

    通常后台传递过来的都是时间戳,但是前台展示不能展示时间戳.就需要转化了. 功能说明: 微信小程序里,时间戳转化为日期格式,支持自定义. 拷贝至项目utils/utils.js中,并注意在js中声明下: ...

  5. js中时间戳与日期格式的相互转换

    1. 将时间戳转换成日期格式: function timestampToTime(timestamp) { var date = new Date(timestamp * 1000);//时间戳为10 ...

  6. js将时间戳转为时间格式

    时间戳转时间格式 //分钟 let timeM= parseInt(msg/1000/60%60); if(timeM<10){ timeM="0"+timeM; } //秒 ...

  7. js时间戳转为日期格式的方法

    Date.prototype.Format = function(fmt){ var o = { "M+" : this.getMonth()+1, //月份 "d+&q ...

  8. js转换时间戳成日期格式

    <script> function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleString().rep ...

  9. JS将时间戳转换为日期格式

    function getDate(time){ var date =(new Date(parseInt(time))).toLocaleDateString() return date; } tim ...

随机推荐

  1. 中国MOOC_零基础学Java语言_第5周 数组

    第5周 数组 5.1 数组 5.2 数组计算 public class Main { public static void main(String[] args) { for (int i = 1; ...

  2. RequestContextHolder获取request和response

    RequestContextHolder获取request和response 2019年03月16日 15:18:15 whp404 阅读数:21更多 个人分类: Spring   首先需要在web. ...

  3. SSTap | ProxyCap

    SSTap  SSTap 全称 SOCKSTap, 是一款利用虚拟网卡技术在网络层实现的代理工具.SSTap 能在网络层拦截所有连接并转发给 HTTP, SOCKS4/5, SHADOWSOCKS(R ...

  4. 你知道e.g.和i.e.的区别吗?

    见 i.e. 是对前面的完全举例,特指 e.g. 则是不完全举例,有可能是...也有可能是...还可能是其他. 注意,i.e. 和 e.g. 第二个点后面都常跟一个逗号.

  5. Html5 Canvas斗地主游戏

    过完年来公司,没什么事,主管说研究下html5 游戏,然后主管就给了一个斗地主的demo,随后我就开始看代码, 现在我看了html5以及canvas相关知识和斗地主的demo后,自己用demo上的素材 ...

  6. itchat监听微信撤回消息

    import itchat from itchat.content import * import re msg_infomation = {} # 监听发送消息 @itchat.msg_regist ...

  7. [19/06/03-星期一] HTML基础_C/S与B/S的区别&标题标签(h1-h6)、段落标签(p)

    一.C/S与B/S的区别 C/S(Client/Server):客户端/服务器 1)一般使用的软件都是C/S架构,比如QQ.360.office365: 2)C表示客户端,用户通过客户端来使用软件:S ...

  8. Java编程思想学习录(连载之:内部类)

    内部类基本概念 可将一个类的定义置于另一个类定义的内部 内部类允许将逻辑相关的类组织在一起,并控制位于内部的类的可见性 甚至可将内部类定义于一个方法或者任意作用域内! 当然,内部类 ≠ 组合 内部类拥 ...

  9. Java8与JDK8和JDK1.8有什么区别?

    JDK版本与发行时间 版本 名称 发行日期 JDK 1.0 Oak(橡树) 1996-01-23 JDK 1.1 none(无) 1997-02-19 JDK 1.1.4 Sparkler(宝石) 1 ...

  10. vscode学习(三)之如何修改打开终端的默认shell

    实现 第一步:打开VSCode的设置(Preferences>User Settings) 第二步:搜索terminal.integrated.shell.osx 的 并把它的值改为你的zsh安 ...