类似于Java日期格式化的JavaScript实现

  1/**
  2@author:Pancras
  3@version:1.01
  4@date:2008-03-02
  5@methods:
  6    DateUtil.getDate(year,month,day)        输入参数为年、月、日,返回一个JS的Date型对象
  7    DateUtil.formateDate(pattern,date)        输入参数一个是格式化字符串,比如"yyyy-MM-dd",和JS日期对象,输出格式化后的日期字符串(基本上和Java的格式化类似)
  8    DateUtil.formateDateTime(pattern,date)    参数类型同上,但是该方法是在要格式化包含时间的时候使用,如"yyyy-MM-dd HH-mm-ss"
  9    DateUtil.isLeapYear(date)                输入一个JS日期型参数,判断该日期所在的年是不是闰年
 10**/
    
 11    
 12    function DateUtil(){
 13        alert("请不要new这个对象,所有方法都是可以通过DateUtil.方法名来调用");
 14    }

 15//-------------------------------------------
 16    //定义类方法getDate(year,month,day)创建对应的日期对象
 17    function getDate(year,month,day)
 18    {
 19        var date = new Date();
 20        if(year)
 21        {
 22            date.setYear(year);
 23        }

 24        if(month)
 25        {
 26            date.setMonth(month-1);
 27        }

 28        if(day)
 29        {
 30            date.setDate(day);
 31        }

 32        return date;
 33    }

 34    DateUtil.getDate=getDate;
 35//-------------------------------------------
 36    //定义日期时间格式化
 37    DateUtil.yearPattern = [new RegExp("y{4}","g"),new RegExp("y{3}","g"),new RegExp("y{2}","g")];
 38    DateUtil.monthPattern = [new RegExp("M{2}","g"),new RegExp("M{1}","g")];
 39    DateUtil.datePattern = [new RegExp("d{2}","g"),new RegExp("d{1}","g")];
 40    DateUtil.hourPattern=[new RegExp("H{2}","g"),new RegExp("H{1}","g"),new RegExp("h{2}","g"),new RegExp("h{1}","g")];
 41    DateUtil.minutePattern=[new RegExp("m{2}","g"),new RegExp("m{1}","g")];
 42    DateUtil.secondPattern=[new RegExp("s{2}","g"),new RegExp("s{1}","g")];
 43    //格式化年
 44    function formatYear(pattern,year)
 45    {            
 46        var result = pattern.match(DateUtil.yearPattern[0]);
 47        if(result!=null){
 48            pattern = pattern.replace(DateUtil.yearPattern[0],year);
 49            return pattern;
 50        }
else {
 51    
 52            //不允许匹配成yyy
 53            result = pattern.match(DateUtil.yearPattern[1]);
 54            if(result!=null){
 55                throw "Unknown pattern:"+pattern;
 56                
 57            }
else {
 58                result = pattern.match(DateUtil.yearPattern[2]);
 59                if(result!=null)
 60                {
 61                    pattern = pattern.replace(DateUtil.yearPattern[2],(""+year).substring(2,4));
 62                    return pattern;
 63                }

 64            }

 65        }

 66    }

 67    //格式化月
 68    function formatMonth(pattern,month)
 69    {
 70        
 71        var result = pattern.match(DateUtil.monthPattern[0]);
 72        if(result!=null){
 73            if(month<10)
 74            {
 75                month = "0"+month;
 76            }

 77            pattern = pattern.replace(DateUtil.monthPattern[0],month);
 78            return pattern;
 79        }
else
 80        {
 81            result = pattern.match(DateUtil.monthPattern[1]);
 82            if(result!=null){
 83                pattern = pattern.replace(DateUtil.monthPattern[1],month);
 84                return pattern;
 85            }

 86            else{
 87                throw "Unknown pattern:"+pattern;
 88            }

 89            
 90        }

 91    }

 92    //格式化日期
 93    function formatDay(pattern,day)
 94    {
 95            
 96        var result = pattern.match(DateUtil.datePattern[0]);
 97        if(result!=null){
 98            if(day<10)
 99            {
100                day = "0"+day;
101            }

102            pattern = pattern.replace(DateUtil.datePattern[0],day);
103            return pattern;
104        }
else
105        {
106            result = pattern.match(DateUtil.datePattern[1]);
107            if(result!=null){
108                pattern = pattern.replace(DateUtil.datePattern[1],day);
109                return pattern;
110            }

111            else{
112                throw "Unknown pattern:"+pattern;
113            }

114            
115        }

116    }

117    //格式化小时
118    function formatHour(pattern,hour)
119    {        
120            var result = pattern.match(DateUtil.hourPattern[0]);
121            if(result!=null){
122                if(hour<10)
123                {
124                    hour = "0"+hour;
125                }

126                pattern = pattern.replace(DateUtil.hourPattern[0],hour);
127                return pattern;
128            }
else
129            {
130                result = pattern.match(DateUtil.hourPattern[1]);
131                if(result!=null){
132                    pattern = pattern.replace(DateUtil.hourPattern[1],hour);
133                    return pattern;
134                }

135                else{
136                        result = pattern.match(DateUtil.hourPattern[2]);
137                        if(result!=null){
138                            if(hour<10)
139                            {
140                                hour = "0"+hour;
141                            }

142                            if(hour>12)
143                            {
144                                 hour = hour-12;
145                            }

146                            pattern = pattern.replace(DateUtil.hourPattern[2],hour);
147                            return pattern;
148                        }
else{
149                            result = pattern.match(DateUtil.hourPattern[3]);
150                            if(result!=null){
151                                if(hour>12)
152                                {
153                                     hour = hour-12;
154                                }

155                                pattern = pattern.replace(DateUtil.hourPattern[3],hour);
156                                return pattern;
157                            }
else{
158                                throw "Unknown pattern:"+pattern;
159                            }

160                        }

161                
162                }

163            }

164    }

165    //格式化分钟
166    function formatMinute(pattern,minute)
167    {
168            
169        var result = pattern.match(DateUtil.minutePattern[0]);
170        if(result!=null){
171            if(minute<10)
172            {
173                minute = "0"+minute;
174            }

175            pattern = pattern.replace(DateUtil.minutePattern[0],minute);
176            return pattern;
177        }
else
178        {
179            result = pattern.match(DateUtil.minutePattern[1]);
180            if(result!=null){
181                pattern = pattern.replace(DateUtil.minutePattern[1],minute);
182                return pattern;
183            }

184            else{
185                throw "Unknown pattern:"+pattern;
186            }

187            
188        }

189    }

190    //格式化秒
191    function formatSecond(pattern,second)
192    {
193            
194        var result = pattern.match(DateUtil.secondPattern[0]);
195        if(result!=null){
196            if(second<10)
197            {
198                second = "0"+second;
199            }

200            pattern = pattern.replace(DateUtil.secondPattern[0],second);
201            return pattern;
202        }
else
203        {
204            result = pattern.match(DateUtil.secondPattern[1]);
205            if(result!=null){
206                pattern = pattern.replace(DateUtil.secondPattern[1],second);
207                return pattern;
208            }

209            else{
210                throw "Unknown pattern:"+pattern;
211            }

212            
213        }

214    }

215    function formateDate(pattern,date)
216    {
217        var oldpattern = pattern;
218        try{
219            pattern = formatYear(pattern,DateUtil.getYear(date));
220            pattern = formatMonth(pattern,DateUtil.getMonth(date));
221            pattern = formatDay(pattern,DateUtil.getDay(date));
222            return pattern;
223        }
catch(err){
224            if(err.match("Unknown pattern")){
225                alert("Unknown pattern:"+oldpattern);
226            }
else{
227                alert(err);
228            }

229            
230        }
    
231    }

232    function formateDateTime(pattern,date)
233    {
234        var oldpattern = pattern;
235        try{
236            pattern = DateUtil.formateDate(pattern,date);
237            pattern = formatHour(pattern,date.getHours());
238            pattern = formatMinute(pattern,date.getMinutes());
239            pattern = formatSecond(pattern,date.getSeconds());
240            return pattern;
241        }
catch(err){
242            if(err.match("Unknown pattern")){
243                alert("Unknown pattern:"+oldpattern);
244            }
else{
245                alert(err);
246            }

247            
248        }
    
249    }

250
251    DateUtil.formateDate = formateDate;
252    DateUtil.formateDateTime = formateDateTime;
253//-------------------------------------------
254    //是不是闰年
255    function isLeapYear(date)
256    {
257        var year = DateUtil.getYear();
258        if(year%100==0)
259        {
260            if(year%400==0)
261            {
262                return true;
263            }

264        }
    
265        if(year%100!=0)
266        {
267            if(year%4==0)
268            {
269                return true;
270            }

271        }
    
272        return false;
273    }

274
275    DateUtil.isLeapYear = isLeapYear;

posted on 2008-03-02 13:49 pancras 阅读(2902) 评论(3)  编辑  收藏

评论

# re: 类似于Java日期格式化的JavaScript实现 2008-03-02 14:31 xifu

不错,多了一条路子  回复  更多评论   

# re: 类似于Java日期格式化的JavaScript实现 2008-03-02 15:04 pancras

哎,英语单词敲错了,抱歉啊!
format是格式化
我写成了formate

使用的时候可以直接这样
DateUtil.formateDate("yyyy-MM-dd",new Date())   回复  更多评论   

# re: 类似于Java日期格式化的JavaScript实现 2008-03-03 10:14 jeasonzhao

不错,学习了,受教了  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年10月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

常用链接

留言簿(1)

随笔档案

搜索

最新评论