function TimeCtrl()
{
this.DateObj = new Date();
this.toGb = function(Str)
{
Str = Str.toString();
if(/^\d+$/.test(Str))
{
var NewStr = "";
var GBNum = "零一二三四五六七八九";
for(var i = 0; i < Str.length; i++)
{
NewStr += GBNum.charAt(Str.charAt(i).valueOf());
}
return NewStr;
}
else
{
return "";
}
}
this.GetYear = function (Format)
{
var Year = this.DateObj.getFullYear().toString();
Format = Format.toString();
if(Format == "yy")
{
return Year.substr(2);
}
else if(Format == "YY")
{
return this.toGb(Year.substr(2));
}
else if(Format == "YYYY")
{
return this.toGb(Year);
}
else
{
return Year;
}
}
this.GetMonth = function (Format)
{
var Month = (parseInt(this.DateObj.getMonth()) + 1).toString();
Format = Format.toString();
if(Format == "mm")
{
if(Month.length == 1)
{
Month = "0" + Month;
}
return Month;
}
else if(Format == "M")
{
return this.toGb(Month);
}
else if(Format == "MM")
{
if(Month.length == 1)
{
Month = "0" + Month;
}
return this.toGb(Month);
}
else
{
return Month;
}
}
this.GetDate = function (Format)
{
var date = parseInt(this.DateObj.getDate()).toString();
Format = Format.toString();
if(Format == "dd")
{
if(date.length == 1)
{
date = "0" + date;
}
return date;
}
else if(Format == "D")
{
return this.toGb(date);
}
else if(Format == "DD")
{
if(date.length == 1)
{
date = "0" + date;
}
return this.toGb(date);
}
else
{
return date;
}
}
this.GetHour = function (Format)
{
var Hour = parseInt(this.DateObj.getHours()).toString();
Format = Format.toString();
if(Format == "hh")
{
if(Hour.length == 1)
{
Hour = "0" + Hour;
}
return Hour;
}
else if(Format == "H")
{
return this.toGb(Hour);
}
else if(Format == "HH")
{
if(Hour.length == 1)
{
Hour = "0" + Hour;
}
return this.toGb(Hour);
}
else
{
return Hour;
}
}
this.GetMinute = function (Format)
{
var Minute = parseInt(this.DateObj.getMinutes()).toString();
Format = Format.toString();
if(Format == "minm")
{
if(Minute.length == 1)
{
Minute = "0" + Minute;
}
return Minute;
}
else if(Format == "MIN")
{
return this.toGb(Minute);
}
else if(Format == "MINM")
{
if(Minute.length == 1)
{
Minute = "0" + Minute;
}
return this.toGb(Minute);
}
else
{
return Minute;
}
}
this.GetSecond = function (Format)
{
var Second = parseInt(this.DateObj.getSeconds()).toString();
Format = Format.toString();
if(Format == "ss")
{
if(Second.length == 1)
{
Second = "0" + Second;
}
return Second;
}
else if(Format == "S")
{
return this.toGb(Second);
}
else if(Format == "SS")
{
if(Second.length == 1)
{
Second = "0" + Second;
}
return this.toGb(Second);
}
else
{
return Second;
}
}
this.GetWeek = function(Format)
{
Format = Format.toString();
Week = parseInt(this.DateObj.getDay()).toString();
if(Format == "W")
{
return this.toGb(Week);
}
else if(Format == "WX")
{
if(Week == "0")
{
return "星期天";
}
else
{
return "星期" + this.toGb(Week);
}
}
else if(Format == "WL")
{
if(Week == "0")
{
return "礼拜天";
}
else
{
return "礼拜" + this.toGb(Week);
}
}
else if(Format == "WZ")
{
if(Week == "0")
{
return "周日";
}
else
{
return "周" + this.toGb(Week);
}
}
else
{
return Week;
}
}
this.IsLeap = function()
{
return (this.DateObj.getFullYear() % 4 == 0 && this.DateObj.getFullYear() % 100 != 0) || this.DateObj.getFullYear() % 400 == 0;
}
this.FormatDate = function(Format)
{
var FormatStr = Format.toString();
FormatStr = FormatStr.replace(/{yy}/g,this.GetYear("yy"));
FormatStr = FormatStr.replace(/{yyyy}/g,this.GetYear("yyyy"));
FormatStr = FormatStr.replace(/{YY}/g,this.GetYear("YY"));
FormatStr = FormatStr.replace(/{YYYY}/g,this.GetYear("YYYY"));
FormatStr = FormatStr.replace(/{m}/g,this.GetMonth("m"));
FormatStr = FormatStr.replace(/{mm}/g,this.GetMonth("mm"));
FormatStr = FormatStr.replace(/{M}/g,this.GetMonth("M"));
FormatStr = FormatStr.replace(/{MM}/g,this.GetMonth("MM"));
FormatStr = FormatStr.replace(/{d}/g,this.GetDate("d"));
FormatStr = FormatStr.replace(/{dd}/g,this.GetDate("dd"));
FormatStr = FormatStr.replace(/{DD}/g,this.GetDate("DD"));
FormatStr = FormatStr.replace(/{D}/g,this.GetDate("D"));
FormatStr = FormatStr.replace(/{w}/g,this.GetWeek("w"));
FormatStr = FormatStr.replace(/{W}/g,this.GetWeek("W"));
FormatStr = FormatStr.replace(/{WX}/g,this.GetWeek("WX"));
FormatStr = FormatStr.replace(/{WZ}/g,this.GetWeek("WZ"));
FormatStr = FormatStr.replace(/{WL}/g,this.GetWeek("WL"));
FormatStr = FormatStr.replace(/{h}/g,this.GetHour("h"));
FormatStr = FormatStr.replace(/{hh}/g,this.GetHour("hh"));
FormatStr = FormatStr.replace(/{H}/g,this.GetHour("H"));
FormatStr = FormatStr.replace(/{HH}/g,this.GetHour("HH"));
FormatStr = FormatStr.replace(/{min}/g,this.GetMinute("min"));
FormatStr = FormatStr.replace(/{minm}/g,this.GetMinute("minm"));
FormatStr = FormatStr.replace(/{MIN}/g,this.GetMinute("MIN"));
FormatStr = FormatStr.replace(/{MINM}/g,this.GetMinute("MINM"));
FormatStr = FormatStr.replace(/{s}/g,this.GetSecond("s"));
FormatStr = FormatStr.replace(/{ss}/g,this.GetSecond("ss"));
FormatStr = FormatStr.replace(/{S}/g,this.GetSecond("S"));
FormatStr = FormatStr.replace(/{SS}/g,this.GetSecond("SS"));
return FormatStr;
}
}
实例一(综合):
平常格式: 中文格式:实例二(FormatDate):
平常格式:中文格式: