(转)javascript日期格式化扩展
转自:http://blog.csdn.net/vbangle/article/details/5643091
方法一:这个很不错,好像是 csdn 的 Meizz 写的:
- // 对Date的扩展,将 Date 转化为指定格式的String
- // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
- // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
- // 例子:
- // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
- // (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
- 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;
- }
调用方法:
- var time1 = new Date().Format("yyyy-MM-dd HH:mm:ss");
- var time2 = new Date().Format("yyyy-MM-dd");
方法二:
- <mce:script language="javascript" type="text/javascript"><!--
- /**
- * 对Date的扩展,将 Date 转化为指定格式的String
- * 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q) 可以用 1-2 个占位符
- * 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
- * eg:
- * (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
- * (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04
- * (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04
- * (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04
- * (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
- */
- Date.prototype.pattern=function(fmt) {
- var o = {
- "M+" : this.getMonth()+1, //月份
- "d+" : this.getDate(), //日
- "h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小时
- "H+" : this.getHours(), //小时
- "m+" : this.getMinutes(), //分
- "s+" : this.getSeconds(), //秒
- "q+" : Math.floor((this.getMonth()+3)/3), //季度
- "S" : this.getMilliseconds() //毫秒
- };
- var week = {
- "0" : "/u65e5",
- "1" : "/u4e00",
- "2" : "/u4e8c",
- "3" : "/u4e09",
- "4" : "/u56db",
- "5" : "/u4e94",
- "6" : "/u516d"
- };
- if(/(y+)/.test(fmt)){
- fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
- }
- if(/(E+)/.test(fmt)){
- fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[this.getDay()+""]);
- }
- 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;
- }
- var date = new Date();
- window.alert(date.pattern("yyyy-MM-dd hh:mm:ss"));
- // --></mce:script>
方法三:
- Date.prototype.format = function(mask) {
- var d = this;
- var zeroize = function (value, length) {
- if (!length) length = 2;
- value = String(value);
- for (var i = 0, zeros = ''; i < (length - value.length); i++) {
- zeros += '0';
- }
- return zeros + value;
- };
- return mask.replace(/"[^"]*"|'[^']*'|/b(?:d{1,4}|m{1,4}|yy(?:yy)?|([hHMstT])/1?|[lLZ])/b/g, function($0) {
- switch($0) {
- case 'd': return d.getDate();
- case 'dd': return zeroize(d.getDate());
- case 'ddd': return ['Sun','Mon','Tue','Wed','Thr','Fri','Sat'][d.getDay()];
- case 'dddd': return ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'][d.getDay()];
- case 'M': return d.getMonth() + 1;
- case 'MM': return zeroize(d.getMonth() + 1);
- case 'MMM': return ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'][d.getMonth()];
- case 'MMMM': return ['January','February','March','April','May','June','July','August','September','October','November','December'][d.getMonth()];
- case 'yy': return String(d.getFullYear()).substr(2);
- case 'yyyy': return d.getFullYear();
- case 'h': return d.getHours() % 12 || 12;
- case 'hh': return zeroize(d.getHours() % 12 || 12);
- case 'H': return d.getHours();
- case 'HH': return zeroize(d.getHours());
- case 'm': return d.getMinutes();
- case 'mm': return zeroize(d.getMinutes());
- case 's': return d.getSeconds();
- case 'ss': return zeroize(d.getSeconds());
- case 'l': return zeroize(d.getMilliseconds(), 3);
- case 'L': var m = d.getMilliseconds();
- if (m > 99) m = Math.round(m / 10);
- return zeroize(m);
- case 'tt': return d.getHours() < 12 ? 'am' : 'pm';
- case 'TT': return d.getHours() < 12 ? 'AM' : 'PM';
- case 'Z': return d.toUTCString().match(/[A-Z]+$/);
- // Return quoted strings with the surrounding quotes removed
- default: return $0.substr(1, $0.length - 2);
- }
- });
- };
(转)javascript日期格式化扩展的更多相关文章
- JavaScript 日期格式化 简单有用
JavaScript 日期格式化 简单有用 代码例如以下,引入jquery后直接后增加下面代码刷新可測试 Date.prototype.Format = function (fmt) { //auth ...
- Javascript 日期格式化
Javascript 日期格式化 需求: 给出:日期 .格式,根据日期格式进行输出. Date.prototype.Format = function (fmt) { //author: meizz ...
- javascript日期格式化方法汇总
本文给大家汇总介绍了javascript格式化日期时间的几种常用方法,个人对最后一种个性化输出时间比较有兴趣,基本上只要项目中能用到都是使用这种,推荐给小伙伴们. 方法一: ? 1 2 3 4 5 6 ...
- js日期格式化 扩展Date()
javascript Date format(js日期格式化) 方法一: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(H/h).分(m).秒(s ...
- 一个JavaScript日期格式化扩展函数
我们都知道在Java和PHP语言中,有专门用于格式化日期对象的类和函数,例如Java中的DateFormat等等,通过这些类和函数,我们可以方便的将一个日期对象按照格式的要求输出为字符串,例如对于同一 ...
- Javascript 日期格式化 相关操作
1.相关扩展函数 //--------------------------------------------------- // 判断闰年 //--------------------------- ...
- Javascript日期格式化指定格式的字符串实现
代码部分 TypeScript /** * format a Date object * 将 Date 转化为指定格式的String * @param {Date} date 源日期对象 * @par ...
- JS日期格式化扩展
1.扩展 //扩展日期 Date.prototype.Format = function (fmt) { //author: meizz var o = { , //月份 "d+" ...
- 轻松搞定javascript日期格式化问题
Date.prototype.format = function(f){ var d = this f = f || "yyyy-MM-dd hh:mm:ss" return f. ...
随机推荐
- python 学习定时任务apscheduler模块
最近在解决定时任务问题找到了apscheduler模块,贴一段代码 from apscheduler.schedulers.blocking import BlockingSchedulerimpor ...
- 《Linux内核设计与实现》笔记-1-linux内核简单介绍
一.Linux内核相对于传统的UNIX内核的比較: (1):Linux支持动态内核模块. 虽然Linux内核也是总体式结构,但是同意在须要的时候动态哦卸除(rmmod xxx)和载入内核模块(insm ...
- mysql 严格模式取消 group by 和 date zore
取消单个库的时间严格模式 set global sql_mode=(select replace(@@sql_mode,'NO_ZERO_IN_DATE,NO_ZERO_DATE',''));
- Hibernate 主配置文件详解
摘要: 版权声明:本文为博主原创文章,如需转载请标注转载地址. 博客地址:http://www.cnblogs.com/caoyc/p/5595870.html 一.主配置文件命名规则 1.默认名称: ...
- hibernate 多对多双向关联
package com.bjsxt.hibernate; import java.util.HashSet; import java.util.Set; import javax.persistenc ...
- SuperMap iClient如何使用WMS地图服务
什么是WMS服务 WMS(Web Map Service,Web 地图服务)服务,该服务符合 OGC(Open Geospatial Consortium,开放地理信息联盟)制定的 WMS 实现规范. ...
- Linux-使用 screen 管理你的远程会话
转自:http://www.ibm.com/developerworks/cn/linux/l-cn-screen/ 你是不是经常需要 SSH 或者 telent 远程登录到 Linux 服务器?你是 ...
- 【Shiro】Apache Shiro架构之权限认证(Authorization)
Shiro系列文章: [Shiro]Apache Shiro架构之身份认证(Authentication) [Shiro]Apache Shiro架构之集成web [Shiro]Apache Shir ...
- android的五大布局(layout)
Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建 筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面.Android的五大布局分别是LinearLa ...
- java通过CLASSPATH读取包内文件
读取包内文件,使用的路径一定是相对的classpath路径,比如a,位于包内,此时可以创建读取a的字节流:InputStream in = ReadFile.class.getResourceAsSt ...