Dear All,
My database will return the time in the format of 24hrs as follows.
2012-04-10T21:30:00
But i need to show only timing in GridView as follows
Retrieving Format Required format
2012-04-10T21:30:00 9:30PM
2012-04-10T09:30:00 9:30AM
I need to extract only timing from this & i need to show in GridView as specified format.
How can i achieve this?
Loading


SenthilkumarRank #387 · 6.5K reputationPosted Apr 10, 2012, 6:01 AM EDT
You can convert the timing as below.
DateTime dt = DateTime.Now;
String.Format("{0:t}", dt);
This will return exactly what you expect...
DateTime.Now.ToString("hh:mm tt")
SIVARank #2,303 · 613 reputationPosted Apr 10, 2012, 9:52 AM EDT
<%#Eval("MenuTimeFrom")%>
Is it possible to customize Here?Jignesh TrivediRank #16 · 50.3K reputationPosted Apr 10, 2012, 8:16 PM EDT
please try, following code in column tag.
hope this help.
SIVARank #2,303 · 613 reputationPosted Apr 10, 2012, 9:36 PM EDT
Dear All,
I tried all these options..
my webservice will return Datetime from the database.
I need to customize & display it in Grid as specified above..
Thank you.
Jignesh TrivediRank #16 · 50.3K reputationPosted Apr 10, 2012, 10:16 PM EDT
are taking your webservice responce in datatable or dataset?
If yes then check the datatype of the column that contain date.
it might your column datatype is string so auto format is not support.
I am trying following code.
code side:
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn();
dc1.DataType = typeof(System.String);
dc1.ColumnName = "Name";
dt.Columns.Add(dc1);
DataColumn dc = new DataColumn();
dc.DataType = typeof(System.DateTime);
dc.ColumnName = "myDate";
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr[0] = "Jignesh";
dr[1] = DateTime.Now;
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
Aspx side:
try and let me if you have any more query.
hope this help.
SIVARank #2,303 · 613 reputationPosted Apr 10, 2012, 11:06 PM EDT
Jignesh TrivediRank #16 · 50.3K reputationPosted Apr 10, 2012, 11:49 PM EDT
In this case RowDataBound event of grid view definately help you to resolve this issue.
Try.
First register RowDataBound event on page load
protected void Page_Load(object sender, EventArgs e)
{
GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);
}
AND
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string Date = e.Row.Cells[5].Text;
DateTime dt = new DateTime();
DateTime.TryParse(Date, out dt);
e.Row.Cells[5].Text = dt.ToShortTimeString();
}
}
In above code e.Row.Cells[5].Text Replce you index. i.e. "5" is replace with your actual index of column in above code.
Hope this help.