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

  1. formatDateTime (unix) { // 转换时间戳
  2. var date = new Date(unix)// 时间戳为10位需*1000,时间戳为13位的话不需乘1000
  3. var Y = date.getFullYear() + '-'
  4. var M = (date.getMonth() + < ? '' + (date.getMonth() + ) : date.getMonth() + ) + '-'
  5. var D = date.getDate() + ' '
  6. var h = date.getHours() + ':'
  7. var m = date.getMinutes() + ':'
  8. var s = date.getSeconds()
  9. return Y + M + D + h + m + s
  10. }
  1.  
  2. //将时间戳改为yyyy-MM-dd HH-mm-ss
  3. function formatDateTime(unix) {
  4. var now = new Date(parseInt(unix) * 1);
  5. now = now.toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
  6. if(now.indexOf("下午") > 0) {
  7. if (now.length == 18) {
  8. var temp1 = now.substring(0, now.indexOf("下午")); //2014/7/6
  9. var temp2 = now.substring(now.indexOf("下午") + 2, now.length); // 5:17:43
  10. var temp3 = temp2.substring(0, 1); // 5
  11. var temp4 = parseInt(temp3); // 5
  12. temp4 = 12 + temp4; // 17
  13. var temp5 = temp4 + temp2.substring(1, temp2.length); // 17:17:43
  14. now = temp1 + temp5; // 2014/7/6 17:17:43
  15. now = now.replace("/", "-"); // 2014-7/6 17:17:43
  16. now = now.replace("/", "-"); // 2014-7-6 17:17:43
  17. }else {
  18. var temp1 = now.substring(0, now.indexOf("下午")); //2014/7/6
  19. var temp2 = now.substring(now.indexOf("下午") + 2, now.length); // 5:17:43
  20. var temp3 = temp2.substring(0, 2); // 5
  21. if (temp3 == 12){
  22. temp3 -= 12;
  23. }
  24. var temp4 = parseInt(temp3); // 5
  25. temp4 = 12 + temp4; // 17
  26. var temp5 = temp4 + temp2.substring(2, temp2.length); // 17:17:43
  27. now = temp1 + temp5; // 2014/7/6 17:17:43
  28. now = now.replace("/", "-"); // 2014-7/6 17:17:43
  29. now = now.replace("/", "-"); // 2014-7-6 17:17:43
  30. }
  31. }else {
  32. var temp1 = now.substring(0,now.indexOf("上午")); //2014/7/6
  33. var temp2 = now.substring(now.indexOf("上午")+2,now.length); // 5:17:43
  34. var temp3 = temp2.substring(0,1); // 5
  35. var index = 1;
  36. var temp4 = parseInt(temp3); // 5
  37. if(temp4 == 0 ) { // 00
  38. temp4 = "0"+temp4;
  39. }else if(temp4 == 1) { // 10 11 12
  40. index = 2;
  41. var tempIndex = temp2.substring(1,2);
  42. if(tempIndex != ":") {
  43. temp4 = temp4 + "" + tempIndex;
  44. }else { // 01
  45. temp4 = "0"+temp4;
  46. }
  47. }else { // 02 03 ... 09
  48. temp4 = "0"+temp4;
  49. }
  50. var temp5 = temp4 + temp2.substring(index,temp2.length); // 07:17:43
  51. now = temp1 + temp5; // 2014/7/6 07:17:43
  52. now = now.replace("/","-"); // 2014-7/6 07:17:43
  53. now = now.replace("/","-"); // 2014-7-6 07:17:43
  54. }
  55. return now;
  56. };
  57. alert(formatDateTime(1516324500000));

  

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

  1. js时间戳怎么转成日期格式

    原文地址:http://www.sufeinet.com/thread-1500-1-1.html js时间戳怎么转成日期格式这个在主群里有朋友§☆釺哖蟲...o问js时间戳怎么转成日期格式 ,他的问 ...

  2. js angular 时间戳转换成日期格式 年月日 yyyy-MM-dd

    昨天写项目,要把时间戳转换成日期格式发给后端 我就去网上找 看到的一些都不是我想要的 索性自己就写了一个如图 下面是angular 模式 $scope.getMyDate = function(str ...

  3. js时间戳转成日期格式

    将时间戳转换成日期格式:// 简单的一句代码var date = new Date(时间戳); //获取一个时间对象 注意:如果是uinx时间戳记得乘于1000.比如php函数time()获得的时间戳 ...

  4. web 前端 常见操作 将时间戳转成日期格式 字符串截取 使用mui制作选项卡

    1.将时间戳转成日期格式: //第一种 function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleString( ...

  5. python txt装换成excel

    工作中,我们需要经常吧一些导出的数据文件,例如sql查出来的结果装换成excel,用文件发送.这次为大家带上python装换excel的脚本 记得先安装wlwt模块,适用版本,python2-3 #c ...

  6. 将序列化成json格式的日期(毫秒数)转成日期格式

    <script> $(function () { loadInfo(); }) function loadInfo() { $.post("InfoList.ashx" ...

  7. CTime,Systemtime的比较还有转换成日期格式。

    vc为我们提供了两种日期型的变量. 一种是CTime.他的缺点就是年份只支持到2038年,以后的日期就不支持啦,如果你的项目有20-30年的寿命,你就选择使用SYSTEMTIME.这个时间函数来进行比 ...

  8. time_t转化成日期格式小工具

    time_t转化成日期格式小工具下载  http://files.cnblogs.com/files/lansan0701/TimeTool.zip

  9. 将序列化成json格式后日期(毫秒数)转成日期格式

    System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaS ...

随机推荐

  1. C#使用SendMessage发送组合键

    有时需要出发菜单功能,例如发送ALT + F打开应用程序的文件菜单,如何使用SendMessage实现呢? 使用用spy++截取的ALT+F的消息内容(如何使用spy++,请熟悉的高手指点下,我使用s ...

  2. bzoj4516

    后缀自动机 留个板子 upd:大概懂了 每次新加入的npRight集合肯定只有最后一个位置,那么求所有长得不一样的子串贡献就是Max-Min+1,因为Right集合只有这一个位置,所以这Max-Min ...

  3. 6-5 Haar特征2

    在遍历的过程中还需要考虑到一个步长的问题.这个模板一次滑动10个像素,那么总共就需要滑动9次.再加上最开始的第0次,合计在水平方向上和竖直方向上分别都有10个这样的模板,总共也就是100个模板.要计算 ...

  4. VMware ESXI虚拟机挂载移动硬盘

    Windows server2008 R2 1.“编辑虚拟机设置”,点击“添加” 2.点击添加“USB控制器”: 3.添加完“USB控制器”以后,再点击添加“USB设备”: 完成即可:如果系统里面不显 ...

  5. hdoj5115【区间DP·基础】

    题意: 有n头wolf排成一排,杀一头wolf回受到受到的伤害=它的本身a[i]+相邻两个b[i-1]+b[i+1].然后杀死第k个位置的wolf的话,k-1和k+1默认相邻(满足的话). 思路: 用 ...

  6. python __builtins__ type类 (69)

    69.'type', 返回对象类型 class type(object) | type(object_or_name, bases, dict) | type(object) -> the ob ...

  7. mysqldump 工具使用详解——参数选项

    mysqldump 简介 mysqldump 是一种用于逻辑备份的客户端工具,它会产生一套能够重新构建数据库或表的SQL语句.所谓逻辑备份:是利用SQL语言从数据库中抽取数据并存于二进制文件的过程.逻 ...

  8. 左耳朵耗子:我对 GitLab 误删除数据库事件的几点思考

    参考链接:https://www.infoq.cn/article/some-thoughts-on-gitlab-accidentally-deleting-database 太平洋时间 2017 ...

  9. 调试的时候禁止chrome缓存图片

    https://blog.csdn.net/yiifaa/article/details/54290047 https://blog.csdn.net/xinghuo0007/article/deta ...

  10. 跟我一起玩Win32开发(2):完整的开发流程

    上一篇中我给各位说了一般人认为C++中较为难的东西——指针.其实对于C++,难点当然不局限在指针这玩意儿上,还有一些有趣的概念,如模板类.虚基类.纯虚函数等,这些都是概念性的东西,几乎每一本C++书上 ...