Strong Coders Community

Where the extreme coders hang out.
Welcome to Strong Coders Community Sign in | Join | Help Messenger
in Search
Strong Coders Community » ASP.NET / .NET Framework » .NET Framework General Discuss... » Re: date conversions

date conversions

Last post 11-29-2006, 10:29 AM by Dean Fiala . 3 replies.
Sort Posts: Previous Next
  •  11-29-2006, 8:30 AM 1294
    • datagridboy123 is not online. Last active: 02-02-2007, 11:42 AM datagridboy123
    • Top 10 Contributor
    • Joined on 11-04-2006
    • Posts 17
    • Points 380

    date conversions

    Hi I am trying to convert the date to MM/DD/YYYY. Currently it is DD/MM/YYYY.  I tried the below code to format it to MM/DD/YYYY. Could some one please help me correct it. Thanks.

     

    arrival = Convert.ToDateTime(txtArrival.Text.ToString("MM/dd/yyyy"),System.IFormatProvider (formatProvider));

    • Post Points: 35
    • Alert the moderators
  •  11-29-2006, 9:54 AM 1295 in reply to 1294
    • Dean Fiala is not online. Last active: 02-10-2007, 9:30 PM Dean Fiala
    • Top 10 Contributor
    • Joined on 10-17-2006
    • Posts 49
    • Points 620
    • MicrosoftMVP Moderator StrongCoders

    Re: date conversions

    So a user enters a date in txtArrival.Text in DD/MM/YYYY format.

    If arrival is a DateTime object "converting" it will only change the value of the date.

    You want to convert the DISPLAY of the date to MM/DD/YYYY format.

    DateTime arrival = DateTime.Parse(txtArrival.Text); // Keep in mind the format of the DateTime is culture dependent, and it will parse it using the default culture unless you specifiy otherwise.

    //This controls how the date time object displays its value 

    Label1.Text = arrival.ToString("MM/dd/yyyy");



     

     

     


    Dean Fiala
    Very Practical Software, Inc.
    • Post Points: 5
    • Alert the moderators
  •  11-29-2006, 10:15 AM 1296 in reply to 1294
    • JamesCurran is not online. Last active: Feb 10, 2007, 10:07 AM JamesCurran
    • Top 10 Contributor
      Male
    • Joined on 11-01-2006
    • Bloomfield, NJ, USA
    • Posts 82
    • Points 1,015
    • StrongCoders

    Re: date conversions

    Dean covered most of it, but since you know the format of the input string, you'd best use ParseExact. 

    DateTime arrival = DateTime.ParseExact(txtArrival.Text, " dd/MM/yyyy ", null );

    The "null" really should be "CultureInfo.CurrentCulture" or some other CultureInfo object, but it should only be used if you use on of the single letter Standard format string, instead of specifying the exact Custom format string.


    Truth,
    James
    • Post Points: 20
    • Alert the moderators
  •  11-29-2006, 10:29 AM 1299 in reply to 1296
    • Dean Fiala is not online. Last active: 02-10-2007, 9:30 PM Dean Fiala
    • Top 10 Contributor
    • Joined on 10-17-2006
    • Posts 49
    • Points 620
    • MicrosoftMVP Moderator StrongCoders

    Re: date conversions

    Actually, since you can never trust those silly users, I'd use DateTime.TryParseExact.

    Dean Fiala
    Very Practical Software, Inc.
    • Post Points: 5
    • Alert the moderators

Powered by Community Server (Commercial Edition), by Telligent Systems