locked
DateFormat RRS feed

  • Question

  • User-797751191 posted

    Hi

    How the below code 1 & 2 works


    1.
    string
    DateString = "11/12/2009"; IFormatProvider culture = new CultureInfo("en-US", true); DateTime dateVal = DateTime.ParseExact(DateString, "yyyy-MM-dd", culture);

    2.

    DateTime m_date = Convert.ToDateTime(txtPostingDate.Text.ToString());
    cmd0.Parameters.AddWithValue("@PostingDate", SqlDbType.DateTime).Value = m_date;


    Thanks
    Monday, July 15, 2019 9:09 AM

Answers

  • User753101303 posted

    Hi,

    "how it works" is a bit confusing. It seems you rather ask about "what it does".

    The first snippet forces a date conversion using a particular country convention (and likely fails because the date pattern doesn't match).

    The second snippert uses the current culture settings (which is IMO better, you can force this on your app or have a logic to set data when the http request starts if your app have support for multiple languages) but still fails if the date is not valid. I would use DateTime.TryParse

    If you start just with Web Forms you could  have a look at the "model binding" feature which allows to work more directly with data (or you start with web forms and try to understand an existing application ?)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 15, 2019 12:03 PM