js格式化时间戳
//js格式化时间戳,转换为时间格式 2017-1-15 4:10:15
function getLocalTime(nS) {
var time = new Date(parseInt(nS) * 1000).toLocaleString();
var reg = new RegExp("(/)", "g");
var reg_time = new RegExp("(上午|下午)", "g");
return time.replace(reg,'-').replace(reg_time,'');
}
console.log(getLocalTime(1484467815));
//格式为 2017-01-15 16:10:15
console.log(formatDate('1484467815'));
function formatDate(obj){
var time = new Date(parseInt(obj) * 1000);
var y = time.getFullYear(); //年
var m = time.getMonth() + 1; //月
if(m < 10){ m = '0' + m }
var d = time.getDate(); //日
if(d < 10){ d = '0' + d }
var h = time.getHours(); //时
if(h < 10){ h = '0' + h }
var mm = time.getMinutes(); //分
if(mm < 10){ mm = '0' + mm }
var s = time.getSeconds(); //秒
if(s < 10){ s = '0' + s }
var timeStr = y+"-"+m+"-"+d+" "+h+":"+mm+":"+s;
return timeStr;
}
//js格式化当前时间 2017-01-15 16:10:15 2017-01-15 17:12:07 2017-01-15
Date.prototype.format = function(fmt) {
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt)) {
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(fmt)){
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
}
}
return fmt;
}
var time1 = new Date(parseInt(1484467815) * 1000).format("yyyy-MM-dd hh:mm:ss");
var time2 = new Date().format("yyyy-MM-dd hh:mm:ss");
var time3 = new Date().format("yyyy-MM-dd");
console.log(time1,time2,time3);
//2017-01-15 16:10:15 2017-01-15 17:12:07 2017-01-15
js格式化时间戳的更多相关文章
- js格式化时间 js格式化时间戳
一个js格式化时间和js格式化时间戳的例子. 代码:/** * 时间对象的格式化; */Date.prototype.format = function(format) { /* * eg:forma ...
- js的 new Date()日期格式化显示以及js获取时间戳
一.日期格式化显示: 对 new Date() 得到日期的进行格式显示扩展,扩展方法如下: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分 ...
- JS 时间函数 / 格式化时间戳
处理时间主要使用时间对象 Date , 其提供两个静态方法 Date.now() //获得当前时间戳 Date.parse() //将字符串转化成时间戳 创建对象 new Date(); // 返回当 ...
- Sublime Text 2 JS 格式化插件 JsFormat的配置使用
(转自http://www.jb51.net/softjc/178401.html) 这里下载这插件包 https://github.com/jdc0589/JsFormat ,点油下角的zip就能下 ...
- Numeral.js – 格式化和操作数字的 JavaScript 库
Numeral.js 是一个用于格式化和操作数字的 JavaScript 库.数字可以格式化为货币,百分比,时间,甚至是小数,千位,和缩写格式,功能十分强大.支持包括中文在内的17种语言. 您可能感兴 ...
- JS格式化数字金额用逗号隔开保留两位小数
JS格式化金额,正则方式修改. /** * 格式化金额 * @param {[type]} v [要转换的数字] * @param {[type]} len [小数点位数,默认2位] * @param ...
- JS基于时间戳写的浏览访问人数
Title:JS基于时间戳写的浏览访问人数 --2013-12-23 14:07 <script language="JavaScript"> var timesta ...
- js 格式化数字,格式化金额:
js 格式化数字,格式化金额: function number_format(number, decimals, dec_point, thousands_sep) { /* * 参数说明: * nu ...
- Sublime Text 2 JS 格式化插件 JsFormat
这里下载这插件包 https://github.com/jdc0589/JsFormat ,点油下角的zip就能下载插件包放到sublime安装目录的DataPackages目录中重新打开sublim ...
随机推荐
- SlidingMenu的使用,结合Fragment(eclipse环境)
首先下载SlidingMenu,有Library和Sample,然后在自己的项目中引入类库(引入智慧北京工作空间的Library),然后V4包会发生冲突,删掉自己项目Libs目录下的V4包即可 侧滑布 ...
- 剑指offer替换空格
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- Entity Framework 学习中级篇1—EF支持复杂类型的实现
本节,将介绍如何手动构造复杂类型(ComplexType)以及复杂类型的简单操作. 通常,复杂类型是指那些由几个简单的类型组合而成的类型.比如:一张Customer表,其中有FristName和Las ...
- ajax 跨域请求资源问题
其实相当的简单:只需要在服务端设置一下响应头: header("Access-Control-Allow-Origin: *");就可以了!! nice,有木有? 下面两句也可以带 ...
- Block 使用场景
转载自:http://blog.csdn.net/totogo2010/article/details/7839061 代码块本质上是和其他变量类似.不同的是,代码块存储的数据是一个函数体.使用代码块 ...
- 绑定网关mac,防arp攻击
netsh i i show innetsh -c i i add neighbors 16 192.168.1.1 08-57-00-51-19-7c
- 转:web_reg_save_param的使用详解
[摘要]利用实际案例说明如何使用Mercury LoadRunner提取包含在 HTML 页内的动态信息并创建参数. [关键词]性能测试,压力测试,Mercury LoadRunner 应用范围 在使 ...
- PAT (Advanced Level) 1061. Dating (20)
简单模拟. #include<stdio.h> #include<string.h> ],s2[],s3[],s4[]; ][]={"MON ", &quo ...
- PAT (Advanced Level) 1056. Mice and Rice (25)
简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm&g ...
- POJ 3648 Wedding
2-SAT,直接选择新娘一侧的比较难做,所以处理的时候选择新郎一侧的,最后反着输出就可以. A和B通奸的话,就建边 A->B'以及B->A’,表示 A在新郎一侧的话,B一定不在:B在新郎一 ...