1

when reading SQl Date time field , only i can take the date with time ..how to get only date in to text box from Ajax or some method.

this is what i need to do

https://i.sstatic.net/n0fgG.jpg

that's how I'm taking the date to text box.

    protected void ddlBatch_SelectedIndexChanged(object sender, EventArgs e)
    {
        String strConnString = ConfigurationManager.ConnectionStrings["CBConnectionString"].ConnectionString;
        const String strQuery = "select ItemIdentityCode, Qty, PurchasingPrice, ExpireDate, DiscountRate, IssueMode,  Principle, Force from DEL_PurchasesLines where BatchNumber = @BatchNumber";
        SqlConnection conPR = new SqlConnection(strConnString);
        SqlCommand cmdPR = new SqlCommand();
        cmdPR.Parameters.AddWithValue("@BatchNumber", ddlBatch.SelectedItem.Value);
        cmdPR.CommandType = CommandType.Text;
        cmdPR.CommandText = strQuery;
        cmdPR.Connection = conPR;
        try
        {
            conPR.Open();
            SqlDataReader sdr = cmdPR.ExecuteReader();
            while (sdr.Read())
            {

                tHFExpiaryDate.Text = sdr["ExpireDate"].ToString();

            }
        }
        catch (Exception ex)
        {
            //throw ex;
        }

        finally
        {
            conPR.Close();
            conPR.Dispose();
        }
    }

3 Answers 3

9

Don't convert the raw value to a string in the first place - it should already be a DateTime:

DateTime date = (DateTime) dsr["ExpireDate"];

Then you can convert it into whatever format you're interested in:

// TODO: Consider specifying the culture too, or specify a standard pattern.
tHFExpiaryDate.Text = date.ToString("MM/d/yyyy");

It's important to separate the question of "How can I get the data from the database in an appropriate type?" from "How should I present the data to the user?"

Sign up to request clarification or add additional context in comments.

1 Comment

Just a side note for what I see the format should be "M/d/yyyy" (10/2/2013 as in the image).
1

Try something like:

DateTime.ParseExact(sdr["ExpireDate"].ToString(), "MM/d/yyyy", CultureInfo.InvariantCulture)

In your sample:

tHFExpiaryDate.Text = DateTime.ParseExact( ((DateTime)dt.Rows[0][0]).ToString("MM/d/yyyy"), "MM/d/yyyy", System.Globalization.CultureInfo.CurrentCulture).ToString("MM/d/yyyy"));

2 Comments

how to use this answer in my scenario.. can you explain please
sdr["ExpireDate"] will be of type of object. Assuming your data is storing valid info we convert it to string, and parse it - finally we provide a format (like month first) it.
0

This always works for me

protected String getDate(string date)
{
    DateTime dDate;
    string sdate = null;
    if (!string.IsNullOrEmpty(date.ToString()))
    {
        dDate = DateTime.Parse(date.ToString());
        sdate = dDate.ToString("dd/MM/yyyy");
        sdate = dDate.ToLongDateString();
    }
    return sdate;
}

Other date format example http://www.dotnetperls.com/datetime-format

1 Comment

Thank you @KnowledgeSeeker really helpful point ,,, including the link .. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.