javascript日期格式化函数,跟C#中的使用方法类似

背景

  日期格式化是编程中最常用的功能之一,C#中的日期格式化功能做的相当强大,方便易用,但JavaScript中却很不给力,也有网友给出了日期格式化方法:http://www.cnblogs.com/donghongtao/archive/2010/01/07/1641145.html,但用起来仍不方便。刚才看到博问中的一个问题(求一JS日期格式化函数,最好跟C#中的使用方法类似),于是就把自己写的一个日期格式化方法分享给大家,欢迎拍砖。

方法代码

  原理就是重写Date的toString方法(跟C#保持一致),对传入的格式化参数内的内容进行替换,代码如下:

Date.prototype.toString=function(format,loc){
    var time={};
    time.Year=this.getFullYear();
    time.TYear=(""+time.Year).substr(2);
    time.Month=this.getMonth()+1;
    time.TMonth=time.Month<10?"0"+time.Month:time.Month;
    time.Day=this.getDate();
    time.TDay=time.Day<10?"0"+time.Day:time.Day;
    time.Hour=this.getHours();
    time.THour=time.Hour<10?"0"+time.Hour:time.Hour;
    time.hour=time.Hour<13?time.Hour:time.Hour-12;
    time.Thour=time.hour<10?"0"+time.hour:time.hour;
    time.Minute=this.getMinutes();
    time.TMinute=time.Minute<10?"0"+time.Minute:time.Minute;
    time.Second=this.getSeconds();
    time.TSecond=time.Second<10?"0"+time.Second:time.Second;
    time.Millisecond=this.getMilliseconds();
    time.Week=this.getDay();

    var MMMArrEn=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
    var MMMArr=["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"];
    var WeekArrEn=["Sun","Mon","Tue","Web","Thu","Fri","Sat"];
    var WeekArr=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];

    var oNumber=time.Millisecond/1000;

    if(format!=undefined && format.replace(/\s/g,"").length>0){
        if(loc!=undefined && loc =="en"){
            MMMArr=MMMArrEn.slice(0);
            WeekArr=WeekArrEn.slice(0);
        }
        format=format
            .replace(/yyyy/ig,time.Year)
            .replace(/yyy/ig,time.Year)
            .replace(/yy/ig,time.TYear)
            .replace(/y/ig,time.TYear)
            .replace(/MMM/g,MMMArr[time.Month-1])
            .replace(/MM/g,time.TMonth)
            .replace(/M/g,time.Month)
            .replace(/dd/ig,time.TDay)
            .replace(/d/ig,time.Day)
            .replace(/HH/g,time.THour)
            .replace(/H/g,time.Hour)
            .replace(/hh/g,time.Thour)
            .replace(/h/g,time.hour)
            .replace(/mm/g,time.TMinute)
            .replace(/m/g,time.Minute)
            .replace(/ss/ig,time.TSecond)
            .replace(/s/ig,time.Second)
            .replace(/fff/ig,time.Millisecond)
            .replace(/ff/ig,oNumber.toFixed(2)*100)
            .replace(/f/ig,oNumber.toFixed(1)*10)
            .replace(/EEE/g,WeekArr[time.Week]);
    }
    else{
        format=time.Year+"-"+time.Month+"-"+time.Day+" "+time.Hour+":"+time.Minute+":"+time.Second;
    }
    return format;
}

var d=new Date();
console.log(d.toString());    //2011-12-29 11:29:43
console.log(d.toString(""));    //2011-12-29 11:29:43
console.log(d.toString("yyyy-MM-dd"));    //2011-12-29
console.log(d.toString("HH:mm:ss"));    //11:29:43
console.log(d.toString("yyyy-MM-dd HH:mm:ss"));    //2011-12-29 11:29:43
console.log(d.toString("yyyy年MM月dd日 HH:mm:ss"));    //2011年12月29日 11:29:43
console.log(d.toString("yyyy-MM-dd HH:mm:ss fff"));    //2011-12-29 11:29:43 862

  代码中TMonth、TDay前的T表示两位数(Two)。

小结

  本人是javascript初学者,欢迎大家讨论拍砖。

posted @ 2011-12-29 11:42  artwl  阅读(2459)  评论(2编辑  收藏  举报

个人简介

var ME = {
	"name": "土豆/Artwl",
	"job": "coding",
	"languages": [
		"JS", "HTML",
                "CSS", "jQuery"
		"MVC",".NET",
		"设计模式"
	],
	"hobby": [
		"阅读", "旅游",
		"音乐", "电影"
	]
}
TOP