js将时间戳装换成日期格式

13位时间戳改为yyyy-MM-dd HH-mm-ss 格式 
目标时间戳:1516324500000文章来源地址https://www.yii666.com/article/758282.html文章地址https://www.yii666.com/article/758282.html网址:yii666.com<网址:yii666.com

formatDateTime (unix) { // 转换时间戳
var date = new Date(unix)// 时间戳为10位需*1000,时间戳为13位的话不需乘1000
var Y = date.getFullYear() + '-'
var M = (date.getMonth() + < ? '' + (date.getMonth() + ) : date.getMonth() + ) + '-'
var D = date.getDate() + ' '
var h = date.getHours() + ':'
var m = date.getMinutes() + ':'
var s = date.getSeconds()
return Y + M + D + h + m + s
}

//将时间戳改为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));

  文章来源地址:https://www.yii666.com/article/758282.html

版权声明:本文内容来源于网络,版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。文本页已经标记具体来源原文地址,请点击原文查看来源网址,站内文章以及资源内容站长不承诺其正确性,如侵犯了您的权益,请联系站长如有侵权请联系站长,将立刻删除

js将时间戳装换成日期格式-相关文章

  1. Java,double类型转换成String,String装换成double型

  2. 将n行3列的数据dataTable装换成m行7列的dataTable

  3. js将时间戳装换成日期格式

  4. Web Api 将DataTable装换成Excel,并通过文件流将其下载

  5. js 一数组分割成若干个数组,并装换成字符串赋个li标签

  6. [CSAPP笔记]Binary , Unsigned , Signed 之间的相互装换

  7. 转:js小技巧 ,将彻底屏蔽鼠标右键,可用于Table ,取消选取、防止复制,IE地址栏前换成自己的图标

    1. oncontextmenu=\\\"window.event.returnValue=false\\\" 将彻底屏蔽鼠标右键table border oncontextmenu=return(false)tdno/table 可用于Table2. body onselectstart=\\\"return false\\\" 取消选取、防止复制3. onpaste=\\\"return false\\\" 不准粘贴4. oncopy=\\\"return false;\\\" oncut=\\\"return false;\\\" 防止复制5. link rel=\\\"Shortcut Icon\\\" href=\\\"favicon.ico\\\" IE地址栏前

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信图片_20190322181744_03.jpg

微信扫一扫打赏

请作者喝杯咖啡吧~

支付宝扫一扫领取红包,优惠每天领

二维码1

zhifubaohongbao.png

二维码2

zhifubaohongbao2.png