全面兼容的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 相关文章推荐
经典的解除许多网站无法复制文字的绝招
Dec 31 Javascript
asp.net和asp下ACCESS的参数化查询
Jun 11 Javascript
js身份证判断方法支持15位和18位
Mar 18 Javascript
jQuery往textarea中光标所在位置插入文本的方法
Jun 26 Javascript
javascript自动恢复文本框点击清除后的默认文本
Jan 12 Javascript
轻松掌握JavaScript装饰者模式
Aug 27 Javascript
JQuery Ajax WebService传递参数的简单实例
Nov 02 Javascript
webuploader分片上传的实现代码(前后端分离)
Sep 10 Javascript
JS/HTML5游戏常用算法之碰撞检测 包围盒检测算法详解【圆形情况】
Dec 13 Javascript
解决Layui数据表格的宽高问题
Sep 28 Javascript
vue 限制input只能输入正数的操作
Aug 05 Javascript
vue中使用腾讯云Im的示例
Oct 23 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
zf框架的registry(注册表)使用示例
2014/03/13 PHP
C#使用PHP服务端的Web Service通信实例
2014/04/08 PHP
php正则表达式基本知识与应用详解【经典教程】
2017/04/17 PHP
php自定义排序uasort函数示例【二维数组按指定键值排序】
2019/06/19 PHP
jQuery 快速结束当前正在执行的动画
2013/11/20 Javascript
利用Keydown事件阻止用户输入实现代码
2014/03/11 Javascript
javacript使用break内层跳出外层循环分析
2015/01/12 Javascript
JavaScript中数据结构与算法(二):队列
2015/06/19 Javascript
jQuery幻灯片带缩略图轮播效果代码分享
2015/08/17 Javascript
全面解析Bootstrap手风琴效果
2020/04/17 Javascript
AngularJS基础 ng-mousemove 指令简单示例
2016/08/02 Javascript
从零开始学习Node.js系列教程之基于connect和express框架的多页面实现数学运算示例
2017/04/13 Javascript
微信小程序实现tab和swiper切换结合效果
2020/07/17 Javascript
详解Webpack+Babel+React开发环境的搭建的方法步骤
2018/01/09 Javascript
vue 实现超长文本截取,悬浮框提示
2020/07/29 Javascript
[01:10:16]DOTA2上海特级锦标赛B组资格赛#2 Fnatic VS Spirit第一局
2016/02/27 DOTA
简单了解Django模板的使用
2017/12/20 Python
Python实现的读取电脑硬件信息功能示例
2018/05/30 Python
Python中一个for循环循环多个变量的示例
2019/07/16 Python
pip安装提示Twisted错误问题(Python3.6.4安装Twisted错误)
2020/05/09 Python
让IE支持CSS3的不完全兼容方案
2014/09/19 HTML / CSS
详解如何在css3打包后自动追加前缀插件:autoprefixer
2018/12/18 HTML / CSS
校领导推荐信
2013/11/01 职场文书
心理健康活动总结
2014/04/30 职场文书
医药销售自荐书
2014/05/29 职场文书
导航工程专业自荐信
2014/09/02 职场文书
研究生简历自我评价范文
2014/09/13 职场文书
工商局局长个人对照检查材料思想汇报
2014/09/23 职场文书
房屋过户委托书范本
2014/10/07 职场文书
团代会闭幕词
2015/01/28 职场文书
英文版辞职信
2015/02/28 职场文书
女性健康讲座主持词
2015/07/04 职场文书
无故旷工检讨书
2015/08/15 职场文书
《只有一个地球》教学反思
2016/02/16 职场文书
python tqdm用法及实例详解
2021/06/16 Python
微前端qiankun改造日渐庞大的项目教程
2022/06/21 Javascript