js将时间戳装换成日期格式
13位时间戳改为yyyy-MM-dd HH-mm-ss 格式
目标时间戳:1516324500000
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));
js将时间戳装换成日期格式的更多相关文章
- js时间戳怎么转成日期格式
原文地址:http://www.sufeinet.com/thread-1500-1-1.html js时间戳怎么转成日期格式这个在主群里有朋友§☆釺哖蟲...o问js时间戳怎么转成日期格式 ,他的问 ...
- js angular 时间戳转换成日期格式 年月日 yyyy-MM-dd
昨天写项目,要把时间戳转换成日期格式发给后端 我就去网上找 看到的一些都不是我想要的 索性自己就写了一个如图 下面是angular 模式 $scope.getMyDate = function(str ...
- js时间戳转成日期格式
将时间戳转换成日期格式:// 简单的一句代码var date = new Date(时间戳); //获取一个时间对象 注意:如果是uinx时间戳记得乘于1000.比如php函数time()获得的时间戳 ...
- web 前端 常见操作 将时间戳转成日期格式 字符串截取 使用mui制作选项卡
1.将时间戳转成日期格式: //第一种 function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleString( ...
- python txt装换成excel
工作中,我们需要经常吧一些导出的数据文件,例如sql查出来的结果装换成excel,用文件发送.这次为大家带上python装换excel的脚本 记得先安装wlwt模块,适用版本,python2-3 #c ...
- 将序列化成json格式的日期(毫秒数)转成日期格式
<script> $(function () { loadInfo(); }) function loadInfo() { $.post("InfoList.ashx" ...
- CTime,Systemtime的比较还有转换成日期格式。
vc为我们提供了两种日期型的变量. 一种是CTime.他的缺点就是年份只支持到2038年,以后的日期就不支持啦,如果你的项目有20-30年的寿命,你就选择使用SYSTEMTIME.这个时间函数来进行比 ...
- time_t转化成日期格式小工具
time_t转化成日期格式小工具下载 http://files.cnblogs.com/files/lansan0701/TimeTool.zip
- 将序列化成json格式后日期(毫秒数)转成日期格式
System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaS ...
随机推荐
- bootstrap-Glyphicons 字体图标
使用的方法: 1 引入 font-awesome.css文件 2 fonts文件夹 Bootstrap 假定所有的图标字体文件全部位于 ../fonts/ 目录内(可以在font-awesome.c ...
- opencord视频截图
参考:https://www.youtube.com/watch?v=Teu9jK6GF6s
- python __builtins__ 函数
dir(__builtins__) 1.'abs', 对传入参数取绝对值 abs(x, /) Return the absolute value of the argument. >>&g ...
- combobox级联检索下拉选择框
1.效果图 2.前端 @{ ViewBag.Title = "Index"; Layout = null; @*自动筛选下拉框*@ <script src="~/S ...
- EOS:dfuse stream 保证不会错过一个心跳
强大的 dfuse history API 给我们带来了高效的链数据获取途径,让我们的 dapp 在用户体验上了一个台阶. 官方示例 不会错过一个心跳 代码分析 函数 pendingActions 待 ...
- P1218 过路费
奋斗了两天,终于写过了这道题...... 这道题不仅要求最短路,还要加上路径上最大的点权: 先用结构体记录点的序号和点的值这是毋庸置疑的:再用另外一个数组来记录当前点权也是可以理解的,毕竟后面要排序: ...
- bzoj3343 教主的魔法【分块入门】By cellur925
题意:维护一个数列,给出维护区间加法,询问区间内大于等于某个值的元素个数. 算法:分块.因为本题第二问显然可以用二分的思想,但是这貌似并不符合区间可加性,线段树好像就不好用了呢.所以本蒟蒻学习了分块. ...
- Incorrect string value: '\xE8\x8B\x8F\xE6\x99\xA8...' for column 'user_name' at row 1
前端插入数据的时候报如下错误: Incorrect string value: '\xE8\x8B\x8F\xE6\x99\xA8...' for column 'user_name' at row ...
- 跟我一起玩Win32开发(10):绘图(C)
今天我们来欣赏一下用于填充图形的函数,当然我不会逐个去介绍,因为我们参考MSDN直接套参数就可以了. SetDCBrushColor函数有必要扯一下,它的声明如下: COLORREF SetDCBru ...
- New Land LightOJ - 1424
New Land LightOJ - 1424 题意:找出01矩阵中最大的完全由0组成的矩阵. 方法: 重点在于转化. 先预处理(i,j)点向上最长能取到的连续的全0条的长度.然后枚举某一行作为矩阵的 ...