Date formatting hell

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • boyindie86
    New Member
    • Jun 2007
    • 31

    Date formatting hell

    Hi Forum,

    I am having a tough time with dealing with formatting of times. I have seen so many posts about this on Google, but not one of them seem to provide me with the expected result.

    My issue is in my code I want to be able to format all my times using GB culture formatting, however my program just falls over and crashes if its in the US format, I want a bit of code that will allow my program to always convert the value returned from the system clock to GB format.

    As it always returns the following error:
    String was not recognized as a valid DateTime.

    sProgStart=Date Time.ParseExact (locVal, "dd/MM/yyyy HH:mm:ss", Nothing)

    I have tried a millions of variations of using the formatProvider, to no avail. Any help would be greatly appreciated.

    Boyindie
  • Christopher Nigro
    New Member
    • Jul 2010
    • 53

    #2
    Here is some code to illustrate some things you can do with the TimeZoneInfo class in .NET 3.5 (sorry, it is in C# but simple enough to convert):

    Code:
    TimeZoneInfo tziGB = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
                string locVal = "7/15/2010";
                DateTime usTime = DateTime.Parse(locVal);
                DateTime sProgStart = TimeZoneInfo.ConvertTime(usTime, tziGB);
                Console.WriteLine(sProgStart.ToString("dd/MM/yyyy HH:mm:ss"));
    I am running code on a machine that is "en-US". I create a local DateTime instance. Since I want GB format, I create a TimeZoneInfo instance for GB and use the static method of the TimeZoneInfo class to convert my local DateTime to GB DateTime. However, you could just as easily use a US DateTime and use the ToString overload to get a GB string representation:

    Code:
    string locVal = "7/15/2010";
                DateTime usTime = DateTime.Parse(locVal);
                Console.WriteLine(usTime.ToString("dd/MM/yyyy HH:mm:ss"));

    Comment

    • boyindie86
      New Member
      • Jun 2007
      • 31

      #3
      Is it only possible to access this from the most recent version of Visual studio, As i can't seem to reference timezoneinfo only timezone?

      Comment

      • Christopher Nigro
        New Member
        • Jul 2010
        • 53

        #4
        Like I said in my original reply - it's in .NET 3.5.

        Comment

        Working...