js 将时间戳转为日期格式
最近项目需要在前端将一个13位的时间戳显示成日期格式,在网上查了很多都不符合要求,只有一个是能满足要求的,在这记录一下,说不定以后还用的着。
13位时间戳改为yyyy-MM-dd HH-mm-ss 格式
目标时间戳:1516324500000
//将时间戳改为yyyy-MM-dd HH-mm-ssfunction 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.cnblogs.com/ajaxlu/p/8983733.html
js 将时间戳转为日期格式相关推荐
- js中时间戳与日期格式的相互转换
下面总结一下js中时间戳与日期格式的相互转换: 1. 将时间戳转换成日期格式: 1 2 3 4 5 6 7 8 9 10 11 12 function timestampToTime(timestam ...
- 微信小程序将时间戳转为日期格式
微信小程序,时间戳转为日期格式 在小程序中使用时间选择器时,获取到的时间可能是一个时间戳,这并不是我们想要的,这时候我们得将获取到的时间戳进行转换 将时间戳转为时间格式 //转换为时间格式format ...
- js 时间戳转为日期格式
什么是Unix时间戳(Unix timestamp): Unix时间戳(Unix timestamp),或称Unix时间(Unix time).POSIX时间(POSIX time),是一种时间表示方 ...
- Vue.js中将时间戳转换为日期格式
什么是时间戳 Unix时间戳(Unix timestamp),或称Unix时间(Unix time).POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月0 ...
- Js时间戳转为日期格式
这个在php+mssql(日期类型为datetime)+ajax的时候才能用到,js需要把时间戳转为为普通格式,一般的情况下可能用不到 [php] view plaincopy <script& ...
- 微信小程序,时间戳转为日期格式
//数据转化 function formatNumber(n) {n = n.toString()return n[1] ? n : '0' + n }/*** 时间戳转化为年 月 日 时 分 秒* ...
- 对于19位数的时间戳转为日期格式
定义一个19位的时间戳 long a = 132495861533240000l; long aa = a - 116445600000000000l; 我也不知为何要去减他. long b = aa ...
- 用JavaScript实现时间戳转为日期格式
- js时间戳与日期格式的相互转换
时间戳是自 1970 年 1 月 1 日(00:00:00 GMT)以来的秒数.它也被称为 Unix 时间戳(Unix Timestamp). 下面总结一下js中时间戳与日期格式的相互转换: 1. 将 ...
最新文章
- Netflix 官方技术博客:个性化分发与推荐,走在前列的 Netflix 是怎么做的?
- 自建git服务器连接Pycharm系列二:在centos7上搭建git服务器
- springboot特点
- 排序方法的分类 算法
- 5.3.3 计算机网络传输层之TCP拥塞控制(慢开始与拥塞避免、快重传和快恢复)
- 前端打印第三方插件_vscode mock服务插件amock教程
- [html] 网页中的友情链接有什么作用?
- 第二章 findxargs
- python环境介绍_Python介绍以及Python环境搭建
- 前端_JavaScript_Vue
- AJPFX总结String类的特点
- Oracle scheduler job管理
- php 读xml的两种方式
- php foreach、while性能比较
- 华为服务器系统图标,服务器图标
- comsol学习中心:建模工作流程
- 阿里云智能语音交互服务-录音文件识别采样率不支持-UNSUPPORTED_SAMPLE_RATE 解决方案
- Barrett reduction算法
- vue项目中实现多语言 vue-i18n处理动态加载后端数据语言
- 写给大一充实,大二不顺,大三迷茫的同学
热门文章
- 使用级联样式表的好处在于_现在都在使用益阳云终端解决方案了,原因就在于这些好处...
- 三星android功能怎么用,三星GALAXY S II升级Android 4.0新功能介绍及使用技巧
- vscode 搭建Linux 内核开发环境
- html的css样式中表示后代选择器,html添加css——样式选择器
- tp5查询字段相加_tp5 sum某个字段相加得到总数的例子
- Vivado之增量编译的实现
- 【 FPGA 】BUFHCE 案例浅析
- 【 MATLAB 】适合初学者的 chirp 理解与推导
- 【 C 】在单链表中插入一个新节点的尝试(二)
- 多少人没熬过45-55这段危险期? 为家人, 请多看一眼!