Answered by:
How to change DateTime format in GridView?

Question
-
User-1355475649 posted
Hello, everyone.
Now I`m building some websites and making a board using GridView.
Here, I have some question.
When I loaded the data from database, I want to show DateTime like this.
If I wrote something today, it showed like this 15:42 only time
And tomorrow, it changed like this 16/08/15 (yyMMdd)
How can I make it??
Please give me your brilliant idea or share some examples on the internet.
Thanks.
Tuesday, August 16, 2016 9:06 AM
Answers
-
User283571144 posted
Hi SuperRyden,
When I loaded the data from database, I want to show DateTime like this.
If I wrote something today, it showed like this 15:42 only time
And tomorrow, it changed like this 16/08/15 (yyMMdd)
How can I make it??
According to your description, I suggest you could loop each record from database and use if condition to check the date value is today or not.
If date value is today, you could use 'DateTime.ToString' method to change the date value format to "HH:mm".
If date value is not today, you could use 'DateTime.ToString' method to change the date value format to "yy/MM/dd".
More details, you could refer to follow codes:
HTML:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="True" AllowPaging="True" Height="787px"> <Columns> <asp:BoundField DataField="Id" HeaderText="Id" /> <asp:TemplateField HeaderText="Date"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Date") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("Date") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Code-behind:
protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("Id", typeof(Int32)); dt.Columns.Add("Date", typeof(DateTime)); dt.Rows.Add(1, DateTime.Today.AddDays(-1)); dt.Rows.Add(2, DateTime.Now); dt.Rows.Add(3, DateTime.Today.AddDays(1)); DataTable dt2 = new DataTable(); dt2.Columns.Add("Id", typeof(Int32)); dt2.Columns.Add("Date", typeof(string)); for (int i = 0; i < dt.Rows.Count; i++) { if (((DateTime)dt.Rows[i]["Date"]).Date == DateTime.Today) { string result = ((DateTime)dt.Rows[i]["Date"]).ToString("HH:mm"); dt2.Rows.Add(i, result); } else { string result = ((DateTime)dt.Rows[i]["Date"]).ToString("yy/MM/dd"); dt2.Rows.Add(i, result); } } GridView1.DataSource = dt2; GridView1.DataBind(); }
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 16, 2016 10:56 AM
All replies
-
User410767947 posted
SELECT RIGHT(CONVERT(CHAR(20), getdate(), 22), 11) 'TIME'
select LTRIM(RIGHT(CONVERT(VARCHAR(20),GETDATE(),100),7)) 'TIME'
SELECT CONVERT(char(5), GETDATE(), 108)Tuesday, August 16, 2016 9:52 AM -
User283571144 posted
Hi SuperRyden,
When I loaded the data from database, I want to show DateTime like this.
If I wrote something today, it showed like this 15:42 only time
And tomorrow, it changed like this 16/08/15 (yyMMdd)
How can I make it??
According to your description, I suggest you could loop each record from database and use if condition to check the date value is today or not.
If date value is today, you could use 'DateTime.ToString' method to change the date value format to "HH:mm".
If date value is not today, you could use 'DateTime.ToString' method to change the date value format to "yy/MM/dd".
More details, you could refer to follow codes:
HTML:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="True" AllowPaging="True" Height="787px"> <Columns> <asp:BoundField DataField="Id" HeaderText="Id" /> <asp:TemplateField HeaderText="Date"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Date") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("Date") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Code-behind:
protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("Id", typeof(Int32)); dt.Columns.Add("Date", typeof(DateTime)); dt.Rows.Add(1, DateTime.Today.AddDays(-1)); dt.Rows.Add(2, DateTime.Now); dt.Rows.Add(3, DateTime.Today.AddDays(1)); DataTable dt2 = new DataTable(); dt2.Columns.Add("Id", typeof(Int32)); dt2.Columns.Add("Date", typeof(string)); for (int i = 0; i < dt.Rows.Count; i++) { if (((DateTime)dt.Rows[i]["Date"]).Date == DateTime.Today) { string result = ((DateTime)dt.Rows[i]["Date"]).ToString("HH:mm"); dt2.Rows.Add(i, result); } else { string result = ((DateTime)dt.Rows[i]["Date"]).ToString("yy/MM/dd"); dt2.Rows.Add(i, result); } } GridView1.DataSource = dt2; GridView1.DataBind(); }
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 16, 2016 10:56 AM -
User-1355475649 posted
Thanks a lot Brando ZWZ!
Your example showed me the way how can I make it! and now it works well!!!
Wednesday, August 17, 2016 5:49 AM -
User-1355475649 posted
Thank you raghu_grdr.
But I want to code behind, not sql :)
And have a good day!
Wednesday, August 17, 2016 5:51 AM