問題描述
使用 c# 將日期時間轉換為日期 (Convert datetime to date using c#)
I have datetime like:
01-Jan-10 12:00:00 AM
I want to get only 01-Jan-10
.I do not know how to convert it. Anyone know help me please, Thanks,
參考解法
方法 1:
If you have a DateTime
object and want to get the string in that format, use myDateTime.ToString("dd-MMM-yy")
. If you have a DateTime
object and want to return a new DateTime
object that is just the date component, use DateTime.Date
.
方法 2:
Like this:
DateTime dt = DateTime.Today; //or whatever
string dateString = dt.ToShortDateString();
You can also use ToString()
to format it however you like. In your case, it would be:
string dateString = dt.ToString("dd-MMM-YY");
方法 3:
use method DateTime.ToShortDateString();
or DateTime.ToLongDateString()
(by Maly、Samuel Neff、Phillip Schmidt、burning_LEGION)