惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
K
Kaspersky official blog
A
Arctic Wolf
Attack and Defense Labs
Attack and Defense Labs
L
LINUX DO - 热门话题
N
News | PayPal Newsroom
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
L
Lohrmann on Cybersecurity
PCI Perspectives
PCI Perspectives
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
The Last Watchdog
The Last Watchdog
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
W
WeLiveSecurity
Know Your Adversary
Know Your Adversary
博客园 - Franky
T
Tenable Blog
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Help Net Security
Help Net Security
WordPress大学
WordPress大学
T
The Exploit Database - CXSecurity.com
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
S
Security Affairs
J
Java Code Geeks
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Apple Machine Learning Research
Apple Machine Learning Research
NISL@THU
NISL@THU
O
OpenAI News
The Cloudflare Blog
月光博客
月光博客
Google Online Security Blog
Google Online Security Blog
V
V2EX
罗磊的独立博客
美团技术团队
博客园 - 三生石上(FineUI控件)
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cyberwarzone
Cyberwarzone
L
LINUX DO - 最新话题
Hacker News - Newest:
Hacker News - Newest: "LLM"
大猫的无限游戏
大猫的无限游戏

博客园 - 网际飞狐

异步服务框架 如何在TFS中用命令行提交更新 CollabNet Subversion 你正确关闭WCF链接了吗? 通过OperationContext添加消息头信息 PHP在II7安装指南 [Google App Engine] Hello, world! 在IIS7中配置使用Python 2008年12月小记(NewSequentialID(),ADO.NET Data Service,Visual Studio Tips,安装Django,JQuery智能感知) [OpenAPI] html标签分析 System.Web.Routing 使用基础 Notes for 2008-11(GetRange, backup,file hash, PostRequest, QueryString) JS通过服务代理调用跨域服务 对硬编码WCF服务的封装(提供服务和客户端调用的封装,调用样例....) Observer Pattern, Delegate and Event How to view the W3WP process by c#? 项目框架概要 2008年10月小记(SQL删除重复记录,生成表结构,字符串特性,statistics io) WinDbg使用摘要
输出自定义日期格式 - 网际飞狐 - 博客园
网际飞狐 · 2009-06-02 · via 博客园 - 网际飞狐

通过实现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();
        }