[转]javascript Date format(js日期格式化)
方法一:这个很不错,好像是 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 Date format(js日期格式化)的更多相关文章
- [荐]javascript Date format(js日期格式化)
cnblog:http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html 方法一: // 对Date的扩展,将 Date ...
- javascript Date format(js日期格式化) 转载
本文转载地址http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html // 对Date的扩展,将 Date 转化为指定格 ...
- coding++ :javascript Date format (js日期格式化)
方式一: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1 ...
- javascript Date format(js日期格式化) (转)
方法一:这个很不错,好像是 csdn 的 Meizz 写的: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) ...
- javascript Date format(js日期格式化)
这个用这比较爽,记录一下// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年( ...
- 【JavaScript】 knockout.js 日期格式化借助【momentjs】
源:Knockout.js 日期格式化 源:momentjs
- js日期格式化 扩展Date()
javascript Date format(js日期格式化) 方法一: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(H/h).分(m).秒(s ...
- 161226、js日期格式化
JavaScript Date format(js日期格式化) 方法一:// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季 ...
- JS获取当前日期时间及JS日期格式化
Js获取当前日期时间: var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份( ...
随机推荐
- Uva 11395 Sigma Function (因子和)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=109329#problem/C 题目在文末 题意:1~n (n:1~1012)中,因子 ...
- leetcode-Excel Sheet Column Title
题目: 把数字转化为excel形式的字符表示.示例:1->A 2->B 3->C ... 26->Z 27->AA... 解题思路: 乍一看有点像进制转换题目,不过细想想 ...
- 第66课 C++中的类型识别
1. 类型识别 (1)在面向对象中可能出现下面的情况 ①基类指针指向子类对象 ②基类引用成为子类对象的别名 ▲静态类型——变量(对象)自身的类型(定义变量类型时类型或参数类型) ▲动态类型——指针(引 ...
- u3d_Shader_effects笔记4 BRDF
1.英文意思 bidirectional reflectance distribution function bidirectional :双向的 reflectance :反射 distributi ...
- HTML 学习笔记 CSS3(Animation)
CSS3动画: 通过CSS3 我们能够创建动画 这可以在许多网页中取代动画图片 Flash动画 以及JavaScript. CSS3 @keyframes 规则如需在 CSS3 中创建动画,您需要学习 ...
- APIO2015泛做
可以在UOJ上提交也可以在bzoj上提交(权限) A. Bali Sculptures 对于前72%的数据,按位考虑,然后跑一点沙茶dp就行了. dp:用f[x][y]表示前x位分为y段是否满足条件. ...
- django自带wsgi server vs 部署uwsgi+nginx后的性能对比
一.下面先交代一下测试云主机 cpu: root@alexknight:/tmp/webbench-1.5# cat /proc/cpuinfo |grep model model : model n ...
- Java的super调用案例: super.getClass()返回的是子类自己
If you override a method from your superclass (or your superclass's superclass etc.), super.theMetho ...
- mvc5 Html.EditorFor html属性有了新变化,和以前的不同了
@Html.EditorFor(model => model.MaxNumber, new { htmlAttributes = new { @min = "1" } })
- Zepto的天坑汇总
前言 最近在做移动端开发,用的是zepto,发现他跟jquery比起来称之为天坑不足为过,但是由于项目本身原因,以及移动端速度要求的情况下,也只能继续用下去. 所以在这里做一下汇总 对img标签空sr ...