0

I am using SQL Server.
In my database table named TOTAL there is column UPDATE whose data type is DATETIME. Its format is 03/20/2020

I pass this value to Label1.text. See below

const string query = "Select Top 1 [uppdate] From total";
cmd.CommandText = query;
con.Open();
using (var rd = cmd.ExecuteReader())
{
    while (rd.Read())
    {
        Label1.Text = "Last Update: " + rd["uppdate"].ToString();

But my site shows 03/20/2020 12:00:00 AM. I want to change it to 20.03.2020 without time.

Abra
  • 19,142
  • 7
  • 29
  • 41
echelon
  • 3
  • 4
  • 1
    Does this answer your question? [convert datetime to date format dd/mm/yyyy](https://stackoverflow.com/questions/5050102/convert-datetime-to-date-format-dd-mm-yyyy) – gunr2171 Mar 23 '20 at 15:28
  • Which dbms are you using? (When it comes to date/time many products are far from ANSI SQL complaint.) – jarlh Mar 23 '20 at 15:30

2 Answers2

1

You can use a custom format string:

 rd["uppdate"].ToString("dd.MM.yyyy"); // case sensitive
  • Hi, thank you for your help. I have already tried your solution. But, it gives this error: No overload for method 'ToString' takes 1 arguments. While I was looking for a solution this error, I found the solution. I updated my post. Thank you! – echelon Mar 23 '20 at 16:57
  • Note: `rd["uppdate"]` [(see docs)](https://learn.microsoft.com/en-us/dotnet/api/system.data.idatarecord.item?view=netframework-4.8#System_Data_IDataRecord_Item_System_String_) returns `System.Object`. Use instead `rd.GetDateTime("uppdate")` [(see docs)](https://learn.microsoft.com/en-us/dotnet/api/system.data.idatarecord.getdatetime?view=netframework-4.8#System_Data_IDataRecord_GetDateTime_System_Int32_) to get a `System.DateTime`; then you can call `ToString` on that. – JohnLBevan Mar 23 '20 at 17:25
0

You can do:

Label1.Text = "Last Update: " + rd["uppdate"].ToString("dd.MM.yyyy", CultureInfo.InvariantCulture);
gwt
  • 2,331
  • 4
  • 37
  • 59