js时间转化
来源:互联网 发布:电子相册软件免费版 编辑:程序博客网 时间:2024/05/22 13:38
一、js时间的转化
1)时间戳转化为特定格式
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().format("yyyy-MM-dd hh:mm:ss");
2. 将指定的日期转换为"年月日"的格式,代码如下:
var oldTime = (new Date("2012/12/25 20:11:11")).getTime(); var curTime = new Date(oldTime).format("yyyy-MM-dd"); console.log(curTime);
运行如下:
3. 将 "时间戳" 转换为 "年月日" 的格式.
比如如下代码:
var da = new Date(1402233166999); var year = da.getFullYear()+'年'; var month = da.getMonth()+1+'月'; var date = da.getDate()+'日'; console.log([year,month,date].join('-'));
阅读全文
0 0
- js 时间的转化
- js 时间转化
- js时间转化
- js时间格式转化
- JS 字符串转化时间类型
- js 字符串转化为时间
- js 时间类型的转化
- 【js】vue时间格式转化
- js时间转化为指定格式时间
- js时间戳转化为时间
- js 时间与时间戳 相互转化
- js转化时间或者时间戳
- js中时间戳转化为时间
- js 日期转化为unix时间戳
- 利用JS实现时间格式转化
- js转化毫秒为时间格式
- js内时间戳日期格式转化
- js时间戳,日期转化处理
- 数组运算重点总结
- Qt自定义类型的注册与使用
- Lua 中使用C#的类
- react native Scrollview在某些时候滚动无效
- mac .m2
- js时间转化
- java程序员进阶之路
- http 与 https
- Linux修改文件/文件夹权限
- java系统环境System
- 【LEFT JOIN 入门级实践】MYSQL 简单实现 ORACLE minus 差集函数
- mysql视图
- 百度地图定位开发流程
- 矩阵- Matrices 线性代数