Adrien Buet
Adrien Buet

Reputation: 191

C# change formatting for a single DateTime

I am creating DateTimes that are used by a third party library (on which I have of course no control). I am using this 3rd party library to write some files, including the DateTimes I am creating.

I would like to print my dates in different format but I have no control on how the DateTime is converted by the third party and I cannot change my culture info between conversion of each DateTime, neither can I inherit DateTime to override ToString (like no one can).

Is there a way to bind a specific formatting to a DateTime so that each call to ToString method will use this formatting ?

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

Upvotes: 0

Views: 352

Answers (5)

Ken Kin
Ken Kin

Reputation: 4693

If you are sure about that the set datetime methods will call ToString() and save it with your third party library, then you can use following class

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);
    }
}

the test code

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);

If your library doesn't save the datetime as formatted string when you call SetFirstDate() or SetSecondDate(), it doesn't work.

Upvotes: 1

Emanuele Greco
Emanuele Greco

Reputation: 12721

It's not possible to set inside a DateTime the format you expect to use for ToString() method.

System.DateTime is a sealed class which means that you cannot extend.
You can, instead, create your own Date class so that you can specify it.

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;
    }
}

so that you can use this way

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

thirdPartyLib must be changed to use DateTimeWithFormat instead of DateTime

Upvotes: 0

Abdusalam Ben Haj
Abdusalam Ben Haj

Reputation: 5433

With the information given in your question, The only way you could solve this is by implementing your own printing library.

or if the 3rd party library is extendable (I doubt it since you mentioned you have no control over it) then override the PrintBothDate() to suit your needs.

Upvotes: 0

Nolonar
Nolonar

Reputation: 6122

Unfortunately that is not possible. You'll have to work around that problem:

thirdPartyLib.SetFirstDate(firstDate);
thirdPartyLib.SetSecondDate(secondDate);
thirdPartyLib.PrintString(firstData.ToString(firstDateFormatting)); //Assuming such a method exists
thirdPartyLib.PrintString(secondDate.ToString(secondDateFormatting));

Upvotes: 0

Kaf
Kaf

Reputation: 33829

Try this (assigning to a string with the specific format);

Each call to ToString method will use this formatting ?

string firstDate = new DateTime(2013, 02, 07).ToString("yyyy-MM-dd");
string secondDate  = new DateTime(2013, 02, 07).ToString("dddd, MMMM dd, yyyy");

Upvotes: 0

Related Questions