输出自定义日期格式

通过实现IFormatProvider, ICustomFormatter接口可以实现自定义的格式输现,这里有实现一个例子,以输出日期格式为例(显示今天、明天、后天和"x月x日"等)

1、假设我们有多种显示日期格式的需求,我们可以定义一个枚举如下:

    public enum DateTimeFormat
    {
        WithDayName,
//含"前天、昨天、今天、明天、后天的显示"
        WithWeek//含星期几的显示
    }

 

2、每种类型实现 IFormatProvider, ICustomFormatter接口。

internal class WithDayNameFormat : IFormatProvider, ICustomFormatter
    {
        
public object GetFormat(Type formatType)
        {
            
if (formatType == typeof(ICustomFormatter))
            {
                
return this;
            }
            
else
            {
                
return Thread.CurrentThread.CurrentCulture.GetFormat(formatType);
            }
        }

        
public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            
string str;
            IFormattable formatable 
= arg as IFormattable;
            
if (formatable == null)
            {   str 
= arg.ToString();}
            
else
            {
                str 
= formatable.ToString(format, formatProvider);
            }
            
if (arg.GetType() == typeof(DateTime))
            {
                DateTime dt 
= (DateTime)arg;
                
int days = (DateTime.Now - dt).Days;
                
switch(days)
                {
                    
case -2:
                        str
="前天";
                        
break;
                    
case -1:
                        str 
= "明天";
                        
break;
                    
case 0:
                        str 
= "今天";
                        
break;
                    
case 1:
                        str 
= "明天";
                        
break;
                    
case 2:
                        str 
= "后天";
                        
break;
                    
default:
                        str 
= string.Format("{0}月{1}日", dt.Month, dt.Day);
                        
break;
                }
            }
            
return str;
        }

    }

 

internal class WithWeekFormat : IFormatProvider, ICustomFormatter
    {
        
public object GetFormat(Type formatType)
        {
            
if (formatType == typeof(ICustomFormatter))
            {
                
return this;
            }
            
else
            {
                
return Thread.CurrentThread.CurrentCulture.GetFormat(formatType);
            }
        }

        
public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            
string str;
            IFormattable formatable 
= arg as IFormattable;
            
if (formatable == null)
            { str 
= arg.ToString(); }
            
else
            {
                str 
= formatable.ToString(format, formatProvider);
            }
            
if (arg.GetType() == typeof(DateTime))
            {
                DateTime dt 
= (DateTime)arg;
                
string dayOfWeef = dt.DayOfWeek.ToString().Trim();
                
switch (dayOfWeef)
                {
                    
case "Monday":
                        dayOfWeef 
= "";
                        
break;
                    
case "Tuesday":
                        dayOfWeef 
= "";
                        
break;
                    
case "Wednesday":
                        dayOfWeef 
= "";
                        
break;
                    
case "Thursday":
                        dayOfWeef 
= "";
                        
break;
                    
case "Friday":
                        dayOfWeef 
= "";
                        
break;
                    
case "Saturday":
                        dayOfWeef 
= "";
                        
break;
                    
case "Sunday":
                        dayOfWeef 
= "";
                        
break;
                }
                
return string.Format("{0}年{1}月{2}日 星期{3}", dt.Year, dt.Month, dt.Day, dayOfWeef);
            }
            
return str;
        }
    }


3、使用扩展方法对使用进行封装

    public static class DateTimeExtention
    {
        
public static string ToCustomFormat(this DateTime dt, DateTimeFormat format)
        {
            
string str;
            
switch (format)
            {
                
case DateTimeFormat.WithDayName:
                    str 
= string.Format(new WithDayNameFormat(), "{0}", dt);
                    
break;
                
case DateTimeFormat.WithWeek:
                      str 
= string.Format(new WithWeekFormat(), "{0}", dt);
                      
break;
                
default:
                    
throw new NotSupportedException("不支持的日期格式类型DateTimeFormat");
                    
break;
            }
            
return str;
        }
    }

 

4、客户端使用:

 

 

        static void Main(string[] args)
        {
            Console.WriteLine(DateTime.Now.AddDays(
-3).ToCustomFormat(DateTimeFormat.WithDayName));
            Console.WriteLine(DateTime.Now.AddDays(
-2).ToCustomFormat(DateTimeFormat.WithDayName));
            Console.WriteLine(DateTime.Now.AddDays(
-1).ToCustomFormat(DateTimeFormat.WithDayName));
            Console.WriteLine(DateTime.Now.AddDays(
0).ToCustomFormat(DateTimeFormat.WithDayName));
            Console.WriteLine(DateTime.Now.AddDays(
1).ToCustomFormat(DateTimeFormat.WithDayName));
            Console.WriteLine(DateTime.Now.AddDays(
2).ToCustomFormat(DateTimeFormat.WithDayName));
            Console.WriteLine(DateTime.Now.AddDays(
3).ToCustomFormat(DateTimeFormat.WithDayName));

            Console.WriteLine(DateTime.Now.AddDays(
-3).ToCustomFormat(DateTimeFormat.WithWeek));
            Console.WriteLine(DateTime.Now.AddDays(
-2).ToCustomFormat(DateTimeFormat.WithWeek));
            Console.WriteLine(DateTime.Now.AddDays(
-1).ToCustomFormat(DateTimeFormat.WithWeek));
            Console.WriteLine(DateTime.Now.AddDays(
0).ToCustomFormat(DateTimeFormat.WithWeek));
            Console.WriteLine(DateTime.Now.AddDays(
1).ToCustomFormat(DateTimeFormat.WithWeek));
            Console.WriteLine(DateTime.Now.AddDays(
2).ToCustomFormat(DateTimeFormat.WithWeek));
            Console.WriteLine(DateTime.Now.AddDays(
3).ToCustomFormat(DateTimeFormat.WithWeek));

            Console.ReadKey();
        }

 

 

 

posted @ 2009-06-02 13:24  网际飞狐  阅读(426)  评论(0编辑  收藏  举报