Learning
Learning

Reputation: 20011

Date not coming right in asp.net webform

I am storing Event date in following format 2012-07-29 - YYYY-MM-DD in ms sql server database and i am want to show Event Date in following format MMMM dd, yyyy but for some reason it keeps coming as 7/29/2012 12:00:00 AM

I am using following code

protected void getEventDetails()
{
 lblDate.Text = getDate(ds.Tables[0].Rows[0]["EventDate"].ToString());
}


protected string getDate(object dt)
{

    string date = String.Format("{0:MMMM dd, yyyy}", dt);
}

I tried to play around but i always keeps me getting the same date for some reason whcih i am not able to understand. Am i doing something wrong.

In database date is stored exactly as 2012-07-29 without any time.

Please advise what is wrong with the code

Upvotes: 0

Views: 116

Answers (2)

Shoaib
Shoaib

Reputation: 1

protected void getEventDetails()
{
     lblDate.Text = getDate(ds.Tables[0].Rows[0]["EventDate"].ToString());
}


protected string getDate(object dt)
{
   return DateTime.Parse(dt.ToString()).ToString("MMMM dd, yyyy");
}

Upvotes: 0

Sam Leach
Sam Leach

Reputation: 12956

DateTime.ToString() takes in as an argument the formatting.

A standard or custom date and time format string

Use

protected string getDate(string dt)
{
    DateTime dateTime = DateTime.Parse(dt);
    string date = dateTime.ToString("MMMM dd, yyyy");
}

Upvotes: 3

Related Questions