0

私はサードパーティのライブラリで使用されるDateTimesを作成しています(もちろん私は制御できません)。このサードパーティ ライブラリを使用して、作成中の DateTimes を含むいくつかのファイルを書き込んでいます。

日付を別の形式で出力したいのですが、DateTime がサード パーティによってどのように変換されるかを制御できません。また、DateTime を変換するたびにカルチャ情報を変更することも、DateTime を継承して ToString をオーバーライドすることもできません。できる)。

特定のフォーマットを DateTime にバインドして、ToString メソッドを呼び出すたびにこのフォーマットが使用されるようにする方法はありますか?

DateTime firstDate = new DateTime(2013, 02, 07); //I would like this DateTime to be printed this way: 2013-02-07
DateTime secondDate = new DateTime(2013, 02, 07); //I would like this DateTime to be printed this way: Thursday, February 07, 2013

thirdPartyLib.SetFirstDate(firstDate);
thirdPartyLib.SetSecondDate(secondDate);
thirdPartyLib.PrintBothDate(); //This method convert both DateTime in strings
4

5 に答える 5

1

ToString()設定された日時メソッドが呼び出してサードパーティのライブラリに保存することが確実な場合は、次のクラスを使用できます

public static class ThirdPartyLibHelper {
    public static void SetSecondDate(DateTime dateTime) {
        Thread.CurrentThread.CurrentCulture=new System.Globalization.CultureInfo("en-Us");
        var dateTimeFormat=Thread.CurrentThread.CurrentCulture.DateTimeFormat;
        dateTimeFormat.SetAllDateTimePatterns(new[] { "" }, 'T');
        dateTimeFormat.SetAllDateTimePatterns(new[] { "yyyy-MM-dd" }, 'd');
        thirdPartyLib.SetSecondDate(dateTime);
    }

    public static void SetFirstDate(DateTime dateTime) {
        Thread.CurrentThread.CurrentCulture=new System.Globalization.CultureInfo("en-Us");
        var dateTimeFormat=Thread.CurrentThread.CurrentCulture.DateTimeFormat;
        dateTimeFormat.SetAllDateTimePatterns(new[] { "" }, 'T');
        dateTimeFormat.SetAllDateTimePatterns(new[] { "dddd, MMMM dd, yyyy" }, 'd');
        thirdPartyLib.SetFirstDate(dateTime);
    }
}

テストコード

DateTime firstDate=new DateTime(2013, 02, 07);
DateTime secondDate=new DateTime(2013, 02, 07);

ThirdPartyLibHelper.SetSecondDate(firstDate);
var secondDateString=secondDate.ToString();

ThirdPartyLibHelper.SetFirstDate(firstDate);
var firstDateString=firstDate.ToString();

Debug.Print("{0}", firstDateString);
Debug.Print("{0}", secondDateString);

SetFirstDate()またはを呼び出したときにライブラリが日時をフォーマットされた文字列として保存しない場合、ライブラリは機能しSetSecondDate()ません。

于 2013-02-07T12:15:29.067 に答える
0

DateTimeメソッドに使用する予定の形式内に設定することはできませんToString()

System.DateTimesealed拡張できないことを意味するクラスです。
代わりに、独自の Date クラスを作成して、指定できるようにすることができます。

public class DateTimeWithFormat
{

  public DateTime Date {get; set;}
  public string Format {get; set;}

  //ToString override using custom format
  public override string ToString 
   {
     return Date.ToString (Format);
     }    

 //Constructor sets date and format
  public DateTimeWithFormat( DateTime date, string format )
   {
     Date= date;
     Format = format;
    }
}

こんな風に使えるように

DateTimeWithFormat firstDate = new DateTimeWithFormat(
           new DateTime(2013, 02, 07),
           "yyyy-MM-dd");
DateTime secondDate = new DateTimeWithFormat(
           new DateTime(2013, 02, 07),
           "dddd") ; 

thirdPartyLibDateTimeWithFormat 代わりに使用するように変更する必要がありますDateTime

于 2013-02-07T10:49:07.463 に答える
0

残念ながら、それは不可能です。その問題を回避する必要があります。

thirdPartyLib.SetFirstDate(firstDate);
thirdPartyLib.SetSecondDate(secondDate);
thirdPartyLib.PrintString(firstData.ToString(firstDateFormatting)); //Assuming such a method exists
thirdPartyLib.PrintString(secondDate.ToString(secondDateFormatting));
于 2013-02-07T10:59:26.590 に答える
0

これを試してください(特定の形式の文字列に割り当てます)。

ToStringメソッドへの各呼び出しは、このフォーマットを使用しますか?

string firstDate = new DateTime(2013, 02, 07).ToString("yyyy-MM-dd");
string secondDate  = new DateTime(2013, 02, 07).ToString("dddd, MMMM dd, yyyy");
于 2013-02-07T10:41:51.460 に答える
0

質問に記載されている情報を使用して、これを解決できる唯一の方法は、独自の印刷ライブラリを実装することです。

または、サードパーティのライブラリが拡張可能である場合(あなたがそれを制御できないと述べたので、私はそれを疑っています)PrintBothDate()、ニーズに合わせてオーバーライドします。

于 2013-02-07T10:56:28.160 に答える