Javascript扩展Date的prototype实现时间format函数 2017-06-29T09:10:00.000Z日期转换
/*时间格式化 公用方法*/
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 myDateStr = new Date(‘2017-06-29T09:10:00.000Z’).format('yyyy-MM-dd hh:mm:ss') //计算时间间隔,如:刚刚,两小时前,1年前
Date.prototype.comparTime = function(in_date) {
var timestamp = Date.parse(new Date());
var d_value = Math.ceil((timestamp - in_date) / 1000);
var d_html = "";
if (d_value < 60) {
d_html = "1分钟内";
} else if (d_value > 60 && d_value < 3600) {
d_html = parseInt(d_value / 60) + "分钟前";
} else if (d_value > 3600 && d_value < 86400) {
d_html = parseInt(d_value / 3600) + "小时前";
} else if (d_value > 86400 && d_value < 604800) {
d_html = parseInt(d_value / 86400) + "天前";
} else if (d_value > 604800 && d_value < 2419200) {
d_html = parseInt(d_value / 604800) + "周前";
} else if (d_value > 2419200 && d_value < 31536000) {
d_html = parseInt(d_value / 2419200) + "月前";
} else if (d_value > 31536000) {
d_html = parseInt(d_value / 31536000) + "年前";
}
return d_html;
};
Javascript扩展Date的prototype实现时间format函数 2017-06-29T09:10:00.000Z日期转换的更多相关文章
- 时间format函数引爆的知识点和年末有话说
年末感慨 转眼之间,一年的最后一天来了. 2017,技术界貌似正在飞跃.多年的量变终于引起了质变. 人工智能,区块链.对此,我很激动,激动着有点害怕,害怕中有点紧张,还有点渴望.未来的至高点,未来的风 ...
- 【小分享】Date对象封装,时间格式化函数time()
今天再来分享下Date的应用知识点 先看效果,类似于php里边的time('yyyy-mm-dd')用法,但是我这里没有完全依照php的参数格式来,如果有需要稍微修改一下就行. 首先,明确需要用到的参 ...
- Javascript扩展String.prototype实现格式金额、格式时间、字符串连接、计算长度、是否包含、日期计算等功能
<script src="Js/jquery-3.1.1.min.js"></script> <script type="text/java ...
- JavaScript之Date日期对象扩展
各种时间加减 收藏起来以备后用 //名称:日期加法函数 //参数:part(year.month.day.hour.minute.second.millisecond) //返回:Date对象 Dat ...
- js扩展Date对象的方法,格式化日期格式(prototype)
扩展:Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, ...
- javaScript系列:js中获取时间new Date()详细介绍
var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年份(4位,1970-????)m ...
- javascript 关于Date 时间类型 处理方法
上一篇博客中和大家分享了关于 字符串转时间类型 这一篇顺便整理下 javascript 中 Date 类型的一些方法 var time = new Date(); var year=time.getY ...
- JavaScript日期时间格式化函数
这篇文章主要介绍了JavaScript日期时间格式化函数分享,需要的朋友可以参考下 这个函数经常用到,分享给大家. 函数代码: //格式化参数说明: //y:年,M:月,d:日,h:时,m分,s:秒, ...
- js日期格式化 扩展Date()
javascript Date format(js日期格式化) 方法一: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(H/h).分(m).秒(s ...
随机推荐
- zabbix用户管理
zabbix用户管理,主要包括用户增删改查.用户报警媒介管理.用户权限管理. 安装完zabbix后,已经自带了两个用户Admin和Guests 超级管理员默认账号:Admin,密码:zabbix,这是 ...
- 利用ConcurrentHashMap来实现一个ConcurrentHashSet
利用ConcurrentHashMap来实现一个ConcurrentHashSet package hashset; import java.io.Serializable; import java. ...
- iOS 7设计备忘单
With the release of iOS 7, app designers and developers will need to adjust their visual language to ...
- cocos2d-x中的坑
1. 设置opacity=0, 子结点不受到影响. FadeOut 也不起作用. 查看代码可以找到一个 cascadeOpacityEnabled 的参数, 默认是关闭的. 打开就可以了. 2. Ta ...
- Appium 输入 & 符号,实际输入&-
2018-10-11 12:27:12:178 - [debug] [MJSONWP] Calling AppiumDriver.setValue() with args: [["& ...
- java 代码的良好习惯
有很多书籍提到了代码开发的良好习惯,但是自己看过后,在开发中并不能每次都想起来.在此处开贴做笔记,以后自己开发的代码,必须符合. 不要在一个代码块的开头把局部变量一次性都声明了(这是c语言的做法),而 ...
- Android launchMode SingleTask newIntent 的问题
前置条件 项目中,采用 MainActivity + 3个 fragment 的模式作为主要框架.MainActivity 使用 singleTask 模式启动.Fragment 采用 show/hi ...
- BitSet的用法
1,BitSet类 大小可动态改变, 取值为true或false的位集合.用于表示一组布尔标志. 此类实现了一个按需增长的位向量.位 set 的每个组件都有一个 boolean 值.用非负的整数 ...
- 319. Bulb Switcher
题目: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ev ...
- mariadb 10.1.26 二进制包安装笔记
报错链接:http://php.upupw.net/ank2discuss/40/6841-e.html mariadb 10.1.26 二进制版本 全名称: mariadb-10.1.26-linu ...