全面兼容的javascript时间格式化函数(比较实用)


Posted in Javascript onMay 14, 2014

全面兼容的javascript时间格式化函数,实用总结!

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8" /> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>js日期格式化</title> 
<script language="javascript" type="text/javascript"> 
/* 
* 时间格式化 
* strDateTime:需要格式化的字符串时间 
* intType:格式化类型 
*/ 
function formatDateTime(strDateTime, intType) { 
var years, month, days, hours, minutes, seconds; 
var newDate, arrDate = new Array(), arrTime = new Array(); 
try { 
if (strDateTime != undefined && strDateTime != null && strDateTime != "") { 
//获取日期和时间数组 
if (strDateTime.indexOf("-") != -1) { 
var item = strDateTime.split(" "); 
arrDate = item[0].toString().split("-"); 
arrTime = item[1].toString().split(":"); 
} else if (strDateTime.indexOf("/") != -1) { 
var item = strDateTime.split(" "); 
arrDate = item[0].toString().split("/"); 
arrTime = item[1].toString().split(":"); 
} 

//处理数据 
if (arrDate != undefined && arrTime != undefined 
&& arrDate.length == 3 && arrTime.length == 3) { 
newDate = new Date( 
parseInt(arrDate[0]), 
parseInt(arrDate[1]), 
parseInt(arrDate[2]), 
parseInt(arrTime[0]), 
parseInt(arrTime[1]), 
parseInt(arrTime[2]) 
); 

switch (Number(intType)) { 
case 1: //格式:yyyy-MM-dd 
years = newDate.getFullYear(); 

month = newDate.getMonth(); 
if (Number(month) < 10) month = "0" + month; 

days = newDate.getDate(); 
if (Number(days) < 10) days = "0" + days; 

newDate = years + "-" + month + "-" + days; 
break; 
case 2: //格式:MM-dd HH:mm 
month = newDate.getMonth(); 
if (Number(month) < 10) month = "0" + month; 

days = newDate.getDate(); 
if (Number(days) < 10) days = "0" + days; 

hours = newDate.getHours(); 
if (Number(hours) < 10) hours = "0" + hours; 

minutes = newDate.getMinutes(); 
if (Number(minutes) < 10) minutes = "0" + minutes; 

newDate = month + "-" + days + 
" " + hours + ":" + minutes; 
break; 
case 3: //格式:HH:mm:ss 
hours = newDate.getHours(); 
if (Number(hours) < 10) hours = "0" + hours; 

minutes = newDate.getMinutes(); 
if (Number(minutes) < 10) minutes = "0" + minutes; 

seconds = newDate.getSeconds(); 
if (Number(seconds) < 10) seconds = "0" + seconds; 

newDate = hours + ":" + minutes + ":" + seconds; 
break; 
case 4: //格式:HH:mm 
hours = newDate.getHours(); 
if (Number(hours) < 10) hours = "0" + hours; 

minutes = newDate.getMinutes(); 
if (Number(minutes) < 10) minutes = "0" + minutes; 

newDate = hours + ":" + minutes; 
break; 
case 5: //格式:yyyy-MM-dd HH:mm 
years = newDate.getFullYear(); 

month = newDate.getMonth(); 
if (Number(month) < 10) month = "0" + month; 

days = newDate.getDate(); 
if (Number(days) < 10) days = "0" + days; 

hours = newDate.getHours(); 
if (Number(hours) < 10) hours = "0" + hours; 

minutes = newDate.getMinutes(); 
if (Number(minutes) < 10) minutes = "0" + minutes; 

newDate = years + "-" + month + "-" + days + 
" " + hours + ":" + minutes; 
break; 
case 6: //格式:yyyy/MM/dd 
years = newDate.getFullYear(); 

month = newDate.getMonth(); 
if (Number(month) < 10) month = "0" + month; 

days = newDate.getDate(); 
if (Number(days) < 10) days = "0" + days; 

newDate = years + "/" + month + "/" + days; 
break; 
case 7: //格式:MM/dd HH:mm 
month = newDate.getMonth(); 
if (Number(month) < 10) month = "0" + month; 

days = newDate.getDate(); 
if (Number(days) < 10) days = "0" + days; 

hours = newDate.getHours(); 
if (Number(hours) < 10) hours = "0" + hours; 

minutes = newDate.getMinutes(); 
if (Number(minutes) < 10) minutes = "0" + minutes; 

newDate = month + "/" + days + 
" " + hours + ":" + minutes; 
break; 
case 8: //格式:yyyy/MM/dd HH:mm 
years = newDate.getFullYear(); 

month = newDate.getMonth(); 
if (Number(month) < 10) month = "0" + month; 

days = newDate.getDate(); 
if (Number(days) < 10) days = "0" + days; 

hours = newDate.getHours(); 
if (Number(hours) < 10) hours = "0" + hours; 

minutes = newDate.getMinutes(); 
if (Number(minutes) < 10) minutes = "0" + minutes; 

newDate = years + "/" + month + "/" + days + 
" " + hours + ":" + minutes; 
break; 
case 9: //格式:yy-MM-dd 
years = newDate.getFullYear(); 
years = years.toString().substr(2, 2); 

month = newDate.getMonth(); 
if (Number(month) < 10) month = "0" + month; 

days = newDate.getDate(); 
if (Number(days) < 10) days = "0" + days; 

newDate = years + "-" + month + "-" + days; 
break; 
case 10: //格式:yy/MM/dd 
years = newDate.getFullYear(); 
years = years.toString().substr(2, 2); 

month = newDate.getMonth(); 
if (Number(month) < 10) month = "0" + month; 

days = newDate.getDate(); 
if (Number(days) < 10) days = "0" + days; 

newDate = years + "/" + month + "/" + days; 
break; 
case 11: //格式:yyyy年MM月dd hh时mm分 
years = newDate.getFullYear(); 

month = newDate.getMonth(); 
if (Number(month) < 10) month = "0" + month; 

days = newDate.getDate(); 
if (Number(days) < 10) days = "0" + days; 

hours = newDate.getHours(); 
if (Number(hours) < 10) hours = "0" + hours; 

minutes = newDate.getMinutes(); 
if (Number(minutes) < 10) minutes = "0" + minutes; 

newDate = years + "年" + month + "月" + days + 
" " + hours + "时" + minutes + "分"; 
break; 
} 
} 
} 
} catch (e) { 
newDate = new Date(); 

return newDate.getFullYear() + "-" + 
(newDate.getMonth() + 1) + "-" + 
newDate.getDate() + " " + 
newDate.getHours() + ":" + 
newDate.getMinutes() + ":" + 
newDate.getSeconds(); 
} 

return newDate; 
} 
</script> 
</head> 
<body> 
<script language="javascript" type="text/javascript"> 
//调用 
document.writeln(formatDateTime("2014/04/16 22:34:45", 11)); 
</script> 
</body> 
</html>
Javascript 相关文章推荐
jquery dialog open后,服务器端控件失效的快速解决方法
Dec 19 Javascript
JS删除字符串中重复字符方法
Mar 09 Javascript
JavaScript去除数组里重复值的方法
Jul 13 Javascript
基于javascript数组实现图片轮播
May 02 Javascript
require.js 加载 vue组件 r.js 合并压缩的实例
Oct 14 Javascript
基于JavaScript实现本地图片预览
Feb 08 Javascript
基于vue实现多引擎搜索及关键字提示
Mar 16 Javascript
Vue 实现前进刷新后退不刷新的效果
Jun 14 Javascript
JavaScript创建表格的方法
Apr 13 Javascript
vue实现淘宝购物车功能
Apr 20 Javascript
详解react组件通讯方式(多种)
May 06 Javascript
Javascript查看大图功能代码实现
May 07 Javascript
js实现图片拖动改变顺序附图
May 13 #Javascript
javascript判断是否按回车键并解决浏览器之间的差异
May 13 #Javascript
js加密解密字符串可自定义密码因子
May 13 #Javascript
JavaScript用Number方法实现string转int
May 13 #Javascript
javascript中expression的用法整理
May 13 #Javascript
JS函数重载的解决方案
May 13 #Javascript
一个JS函数搞定网页标题(title)闪动效果
May 13 #Javascript
You might like
fleaphp下不确定的多条件查询的巧妙解决方法
2008/09/11 PHP
php实现图片添加水印功能
2014/02/13 PHP
微信公众号判断用户是否已关注php代码解析
2016/06/24 PHP
5 cool javascript apps
2007/03/24 Javascript
复制小说文本时出现的随机乱码的去除方法
2010/09/07 Javascript
jQuery实现的Email中的收件人效果(按del键删除)
2011/03/20 Javascript
jquery简单实现滚动条下拉DIV固定在头部不动
2013/11/25 Javascript
js实现点击图片将图片地址复制到粘贴板的方法
2015/02/16 Javascript
JS使用正则截取两个字符串之间的字符串实现方法详解
2017/01/06 Javascript
ES6扩展运算符的用途实例详解
2017/08/20 Javascript
Vue.js 表单控件操作小结
2018/03/29 Javascript
详解搭建es6+devServer简单开发环境
2018/09/25 Javascript
Nuxt 项目性能优化调研分析
2020/11/07 Javascript
[49:13]DOTA2上海特级锦标赛C组资格赛#1 OG VS LGD第一局
2016/02/27 DOTA
[03:54]Ehome出征西雅图 回顾2016国际邀请赛晋级之路
2016/08/02 DOTA
Python爬取国外天气预报网站的方法
2015/07/10 Python
python 系统调用的实例详解
2017/07/11 Python
Python单体模式的几种常见实现方法详解
2017/07/28 Python
使用C++扩展Python的功能详解
2018/01/12 Python
Python3 批量扫描端口的例子
2019/07/25 Python
Python解析json代码实例解析
2019/11/25 Python
python 字典访问的三种方法小结
2019/12/05 Python
Python json模块与jsonpath模块区别详解
2020/03/05 Python
在django admin详情表单显示中添加自定义控件的实现
2020/03/11 Python
基于Keras中Conv1D和Conv2D的区别说明
2020/06/19 Python
python 删除excel表格重复行,数据预处理操作
2020/07/06 Python
拿来就用!Python批量合并PDF的示例代码
2020/08/10 Python
Chain Reaction Cycles芬兰:世界上最大的在线自行车商店
2017/12/06 全球购物
存储过程和sql语句的优缺点
2014/07/02 面试题
乡村文明行动实施方案
2014/03/29 职场文书
秋天的图画教学反思
2014/05/01 职场文书
工作经历证明书范文
2014/11/02 职场文书
2015年财务试用期工作总结
2014/12/24 职场文书
Python读取文件夹下的所有文件实例代码
2021/04/02 Python
Python字符串对齐方法使用(ljust()、rjust()和center())
2021/04/26 Python
自动在Windows中运行Python脚本并定时触发功能实现
2021/09/04 Python