Anggur Solutions Anggur Solutions - 17 days ago 6
ASP.NET (C#) Question

Converting timestamp to date ONLY

How to convert timestamp to datetime in c#, but only want the date not included the hour minutes and seconds?

by using this code i have full datetime:

double timestamp = Convert.ToDouble(timestamp.Text);
System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
dateTime = dateTime.AddSeconds(timestamp);
lblDate.Text = dateTime.ToString(); //Latest Timestamp


just because I just want the date only I remove

dateTime = dateTime.AddSeconds(timestamp);


and change this line

System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);


to:

System.DateTime dateTime = new System.DateTime(1970, 1, 1);


then the result that i got for date is not correct

Answer

If you want just date part you can use DateTime.ToShortDateString() or DateTime.ToLongDateString() method:

lblDate.Text = dateTime.ToShortDateString();

You can also use Standard or Custom DateTime format string. E.g.

lblDate.Text = dateTime.ToString("d"); // same as above
lblDate.Text = dateTime.ToString("D"); // same as ToLongDateString
lblDate.Text = dateTime.ToString("dd-MMM-yyyy");