Js 时间与字符串转示例
2015-12-31 15:19
609 查看
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Js 时间与字符串转示例</title> <script type="text/javascript"> 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]]; }); } Date.prototype.Format = function (fmt) { //author: meizz 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; } /*-----日期转字符串----start-----------*/ var time2 = new Date().Format("yyyy-MM-dd hh:mm:ss"); alert(time2); /*-----日期转字符串----end-----------*/ /*-----字符串转日期----start-----------*/ var str = '2012-08-12 23:13:15'; str = str.replace(/-/g, "/"); var date = new Date(str); alert(date.getFullYear()); /*-----字符串转日期----end-----------*/ </script> </head> <body> </body> </html>
相关文章推荐
- js函数 Number()、parseInt()、parseFloat()的区别:
- js动态获取图片长宽尺寸(兼容所有浏览器,速度极快)
- 常见的javascript跨域通信方法
- JavaScript 学习日记第一棒
- JS中使用EL表达式
- JSP用common-fileupload实现文件上传下载
- javascript实现input file上传图片预览效果
- 使用SiteMesh分割、装饰jsp页面
- 用JavaScript将数字转换为大写金额
- [LeetCode][JavaScript]Wiggle Sort II
- Java、C++、Python、Ruby、PHP、C#和JavaScript的理解
- 分享几种比较简单实用的JavaScript tabel切换
- js页面跳转整理
- Session监听(Action 、jsp过滤)
- javascript禁止表单输入
- EL标签和JSTL标签---JSP页面的应用
- js simple drag.
- jsp带参转链接
- 纯js 连连看,多套图片连连看
- Extjs5新特性