JavaScript常用方法整理收集
2017-02-13 16:10
405 查看
1.时间处理以及时间格式化---timeformat
方法介绍:根据需求将后台时间字符串转换为需要的时间格式进行显示
参数说明;date:时间字符串 format:需要显示的时间格式规则
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script>
function formatDate(date, format) {
if (!date) return;
if (!format) format = "yyyy-MM-dd";
switch(typeof date) {
case "string":
date = new Date(date.replace(/-/, "/"));
break;
case "number":
date = new Date(date);
break;
}
if (!date instanceof Date) return;
var dict = {
"yyyy": date.getFullYear(),
"M": date.getMonth() + 1,
"d": date.getDate(),
"H": date.getHours(),
"m": date.getMinutes(),
"s": date.getSeconds(),
"MM": ("" + (date.getMonth() + 101)).substr(1),
"dd": ("" + (date.getDate() + 100)).substr(1),
"HH": ("" + (date.getHours() + 100)).substr(1),
"mm": ("" + (date.getMinutes() + 100)).substr(1),
"ss": ("" + (date.getSeconds() + 100)).substr(1)
};
return format.replace(/(yyyy|MM?|dd?|HH?|ss?|mm?)/g, function() {
return dict[arguments[0]];
});
}
alert(formatDate("2010-04-30", "yyyy-MM-dd HH:mm:ss"));
alert(formatDate("2010-4-29 1:50:00", "MM-dd HH:mm:ss"));
</script>
</head>
<body>
</body>
</html>
方法介绍:根据需求将后台时间字符串转换为需要的时间格式进行显示
参数说明;date:时间字符串 format:需要显示的时间格式规则
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script>
function formatDate(date, format) {
if (!date) return;
if (!format) format = "yyyy-MM-dd";
switch(typeof date) {
case "string":
date = new Date(date.replace(/-/, "/"));
break;
case "number":
date = new Date(date);
break;
}
if (!date instanceof Date) return;
var dict = {
"yyyy": date.getFullYear(),
"M": date.getMonth() + 1,
"d": date.getDate(),
"H": date.getHours(),
"m": date.getMinutes(),
"s": date.getSeconds(),
"MM": ("" + (date.getMonth() + 101)).substr(1),
"dd": ("" + (date.getDate() + 100)).substr(1),
"HH": ("" + (date.getHours() + 100)).substr(1),
"mm": ("" + (date.getMinutes() + 100)).substr(1),
"ss": ("" + (date.getSeconds() + 100)).substr(1)
};
return format.replace(/(yyyy|MM?|dd?|HH?|ss?|mm?)/g, function() {
return dict[arguments[0]];
});
}
alert(formatDate("2010-04-30", "yyyy-MM-dd HH:mm:ss"));
alert(formatDate("2010-4-29 1:50:00", "MM-dd HH:mm:ss"));
</script>
</head>
<body>
</body>
</html>
相关文章推荐
- 日常收集整理的JavaScript常用函数方法
- 日常收集整理的JavaScript常用函数方法
- javascript常用函数方法整理汇总 推荐
- javascript常用方法函数收集
- javascript常用的方法整理
- javascript常用方法整理;(不断更新)
- JavaScript 深入学习及常用工具方法整理 ---- 01.浮点数
- 我收集的Javascript常用方法和技巧
- Node.js中JavaScript操作MySQL的常用方法整理
- ASP.NET中常用的优化性能的方法(转贴,Icyer收集整理)
- 常用Javascript方法收集
- javascript常用方法函数收集
- Node.js中JavaScript操作MySQL的常用方法整理
- javaScript常用方法收集
- FusionCharts JavaScript API - Functions 常用方法整理笔记
- javascript常用方法函数收集
- 常用javascript方法整理
- javascript常用方法函数收集
- JavaScript 经典实例日常收集整理(常用经典)
- javascript中常用的方法(总结的较好,收集)