js 将时间戳转为日期格式
最近项目需要在前端将一个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 将时间戳转为日期格式的更多相关文章
- js 时间戳转为日期格式
原文:js 时间戳转为日期格式 js 时间戳转为日期格式 什么是Unix时间戳(Unix timestamp): Unix时间戳(Unix timestamp),或称Unix时间(Unix time) ...
- js时间戳转为日期格式
转自:http://wyoojune.blog.163.com/blog/static/57093325201131193650725/ 这个在php+mssql(日期类型为datetime)+aja ...
- 微信小程序,时间戳转为日期格式
//数据转化 function formatNumber(n) { n = n.toString() ] ? n : ' + n } /** * 时间戳转化为年 月 日 时 分 秒 * number: ...
- 微信小程序时间戳转为日期格式
通常后台传递过来的都是时间戳,但是前台展示不能展示时间戳.就需要转化了. 功能说明: 微信小程序里,时间戳转化为日期格式,支持自定义. 拷贝至项目utils/utils.js中,并注意在js中声明下: ...
- js中时间戳与日期格式的相互转换
1. 将时间戳转换成日期格式: function timestampToTime(timestamp) { var date = new Date(timestamp * 1000);//时间戳为10 ...
- js将时间戳转为时间格式
时间戳转时间格式 //分钟 let timeM= parseInt(msg/1000/60%60); if(timeM<10){ timeM="0"+timeM; } //秒 ...
- js时间戳转为日期格式的方法
Date.prototype.Format = function(fmt){ var o = { "M+" : this.getMonth()+1, //月份 "d+&q ...
- js转换时间戳成日期格式
<script> function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleString().rep ...
- JS将时间戳转换为日期格式
function getDate(time){ var date =(new Date(parseInt(time))).toLocaleDateString() return date; } tim ...
随机推荐
- flultter listview异常type '(BuildContext, int) => dynamic' is not a subtype of type '(BuildContext, int) => Widget'
type '(BuildContext, int) => dynamic' is not a subtype of type '(BuildContext, int) => Widget' ...
- RequestContextHolder
JShop简介:jshop是一套使用Java语言开发的B2C网店系统,致力于为个人和中小企业提供免费.好用的网店系统. 项目主页:http://git.oschina.net/dinguangx/js ...
- C 语言结构体 struct 及内存对齐
struct 结构体 对于复杂的数据类型(例如学生.汽车等),C 语言允许我们将多种数据封装到一起,构成新类型. 跟面向对象语言中的对象相比,结构体只能包含成员变量,不支持操作. #include & ...
- yum基本使用方法
yum 是 Fedora RHEL Centos SUSE等linux 发行版的 软件包管理工具 通过 执行 man yum 查看yum的帮助信息 可以知道 yum makecache 是将服务器上的 ...
- 手把手教你搭建一个 Elasticsearch 集群
为何要搭建 Elasticsearch 集群 凡事都要讲究个为什么.在搭建集群之前,我们首先先问一句,为什么我们需要搭建集群?它有什么优势呢? 高可用性 Elasticsearch 作为一个搜索引擎, ...
- Matlab——程序设计
M文件 我们之前所做的运算————> 算式不太长,或想以交谈式方式进行运算 如果算式很长或是需要一再执行的算式————> 采用M文件的方式 [将指令及算式写成巨集程式然后储存成一个特别的文 ...
- 应用安全 - 无文件式攻击 - 潜伏型攻击 - WMI - 汇总
wbemtest.exe Windows XP Windows 10
- 解决172.17 或者172.18 机房环境下harbor服务器不通的问题
直接改docker-compose.yml文件: 把原来的network选项注释掉,自定义 #networks: # harbor: # external: false networks: harbo ...
- 根据对象属性查找对象或者数组(根据对象属性查找某数组内符合该条件的对象,数组内对象属性check为true的对象,存放到数组内) 滚动轴样式
1.根据对象属性查找某数组内符合该条件的对象. optionComwords:[ {optionName:"名称1", optionCode: '1'}, {optionNam ...
- oracle中与mysql中的命令 show databases, show tables, desc table类似的命令集
1 怎样执行一个sql脚本文件,这个脚本文件写了一系列的sql语句集,比如sql.sql 放在D:\MyEclipse 8.6\Workspaces\OASystem\WebRoot\sql.sql下 ...