js时间格式化函数(兼容IOS)
* 时间格式化
* @param {Object} dateObj 时间对象
* @param {String} fmt 格式化字符串
*/
dateFormat(dateObj, fmt) {
let date;
if (this.isString(dateObj))
{
date = this.strToDate(dateObj)
} else if (this.isDate(dateObj))
{
date=dateObj
}
else{
return ""
}
var o = {
"M+": date.getMonth() + 1, //月份
"d+": date.getDate(), //日
"h+": date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, //小时
"H+": date.getHours(), //小时
"m+": date.getMinutes(), //分
"s+": date.getSeconds(), //秒
"q+": Math.floor((date.getMonth() + 3) / 3), //季度
"S": date.getMilliseconds() //毫秒
};
var week = {
"0": "日",
"1": "一",
"2": "二",
"3": "三",
"4": "四",
"5": "五",
"6": "六"
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear().toString() + "").substr(4 - RegExp.$1.length));
}
if (/(E+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "星期" : "周") : "") + week[date.getDay().toString() + ""]);
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k].toString()) : (("00" + o[k].toString()).substr(("" + o[k].toString()).length)));
}
}
return fmt;
}
/**
* 字符串转换为时间
* @param {String} src 字符串
*/
strToDate(dateObj){
dateObj = dateObj.replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '').replace(/(-)/g, '/')
if (dateObj.indexOf(".")>0)dateObj = dateObj.slice(0, dateObj.indexOf(".")) return new Date(dateObj)
}
使用方法:
第一个参数是个obj对象,当前只能是时间对象或者是时间字符串
第二个参数是格式化字符串,和C# 格式化规则一样,除了周的格式化,这里使用了'E'来格式化周几
这里是页面调用例子:
app.Tools.dateFormat("yyyy/MM/dd") 结果 2017/07/19
app.Tools.dateFormat("yyyy/MM/dd HH:mm:ss") 结果 2017/07/19 10:09:11
app.Tools.dateFormat("yyyy/MM/dd EE HH:mm:ss") 结果 2017/07/19 周三 10:09:11
其他的就不一一举例了,可以参考C#格式化的标准
js时间格式化函数(兼容IOS)的更多相关文章
- js 时间格式化 (兼容safari)
js 时间格式化,兼容IE8和safari浏览器. function formatDate(date, fmt, near, type) { var dateStr = date; if (!date ...
- js时间格式化函数,支持Unix时间戳
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- JS 时间格式化函数
//时间格式化函数 Date.prototype.format = function (format) { var o = { "M+": this.getMonth() + 1, ...
- JS时间格式化函数
Date.prototype.format = function (format) { var o = { "M+": this.getMonth() + 1, //month & ...
- SQL 时间格式化函数发布
SQL 时间格式化函数,有时候因某种需要需要格式化成需要的时间格式,需要的朋友可以收藏下,以备后用. SQL Server里面可能经常会用到的日期格式转换方法: sql server使用convert ...
- js 日期格式化函数(可自定义)
js 日期格式化函数 DateFormat var DateFormat = function (datetime, formatStr) { var dat = datetime; var str ...
- mysql时间格式化函数日期格式h和H区别
本文为博主原创,未经允许不得转载: 今天碰到一个问题,发现项目中有一个统计图的数据和时间格式没有对应准确,统计图要描述的是操作次数和操作时间的关系, 但很奇怪的是操作次数对应的时间却是凌晨,实际应用中 ...
- JavaScript日期时间格式化函数
这篇文章主要介绍了JavaScript日期时间格式化函数分享,需要的朋友可以参考下 这个函数经常用到,分享给大家. 函数代码: //格式化参数说明: //y:年,M:月,d:日,h:时,m分,s:秒, ...
- 时间戳显示为多少分钟前,多少天前的JS处理,JS时间格式化,时间戳的转换
var dateDiff = function (timestamp) { // 补全为13位 var arrTimestamp = (timestamp + '').split(''); for ( ...
随机推荐
- Cyclic Components CodeForces - 977E(找简单环)
题意: 就是找出所有环的个数, 但这个环中的每个点都必须只在一个环中 解析: 在找环的过程中 判断度数是否为2就行...emm... #include <bits/stdc++.h> us ...
- 洛谷 P2862 [USACO06JAN]把牛Corral the Cows 解题报告
P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...
- luogu1373 小a和uim之大逃离 (dp)
直接设f[i][j][k][l][2]是在(i,j)时两人分数是k,l,复杂度会爆掉 但其实只需要知道两人分数只差就行了 所以设f[i][j][k][2],k是分数之差%(K+1),最后一位表示该谁走 ...
- 日志备份的shell脚本
以前工作中写的日志备份的脚本,现记录一下日志备份脚本代码,以后工作中遇到遇到需要备份或者清理日志的时候可以拿来简单修改一下使用,减少工作量. 把备份脚本添加到Linux定时任务中,可以定时执行. 日志 ...
- 「Vue」父子组件之间的传值及调用方法
a.父组件向子组件传值data(){},props数据区别data中的数据可读可写,是自己的数据props是个数组,中的数据是父组件传递过来的,只读不能写<login :dmsg='msg'&g ...
- sqoop的安装与配置
最近需要将MySQL的数据导出到HDFS,所以搜到了sqoop2.跟sqoop1相比,sqoop2的好处是直接使用程序连接到集群上的sqoop,远程操作.流程是需要先创建link也可以理解成要操作的对 ...
- mysql concat_ws 与 concat 多字段模糊匹配应用
有时我们希望在表中多个字段检索是否有我们给出的关键字,我们可以使用 concat_ws 和 concat 连接需要检索的字段,如: select * from userInfo where conca ...
- ceilometer主要组件分析
一.Agent 主要有compute agent 和central agent,还有一些其他的agent这里暂时不分析. agent初始化时会动态加载给定namespace的pollster插件,并通 ...
- python---django请求-响应的生命周期(FBV和CBV含义)
Django请求的生命周期是指:当用户在访问该url路径是,在服务器Django后台都发生了什么. 客户端发送Http请求给服务端,Http请求是一堆字符串,其内容是: 访问:http://crm.o ...
- TED_Topic10:The case for engineering our food
By Pamela Ronald Pamela Ronald studies the genes that make plants more resistant to disease and stre ...