I have a DateTime
instance that has a Date and a Time. How do I extract only the date or only the time?
我有一个具有日期和时间的DateTime实例。如何仅提取日期或仅提取时间?
5 个解决方案
#1
77
var day = value.Date; // a DateTime that will just be whole days
var time = value.TimeOfDay; // a TimeSpan that is the duration into the day
#2
27
You can also use DateTime.Now.ToString("yyyy-MM-dd")
for the date, and DateTime.Now.ToString("hh:mm:ss")
for the time.
您还可以使用DateTime.Now.ToString(“yyyy-MM-dd”)作为日期,使用DateTime.Now.ToString(“hh:mm:ss”)作为时间。
#3
16
You can use Instance.ToShortDateString() for the date,
and Instance.ToShortTimeString() for the time to get date and time from the same instance.
您可以使用Instance.ToShortDateString()作为日期,使用Instance.ToShortTimeString()获取来自同一实例的日期和时间。
#4
1
var currentDateTime = dateTime.Now();
var date=currentDateTime.Date;
#5
-1
Sometimes you want to have your GridView as simple as:
有时您希望GridView简单如下:
<asp:GridView ID="grid" runat="server" />
You don't want to specify any BoundField, you just want to bind your grid to DataReader. The following code helped me to format DateTime in this situation.
您不想指定任何BoundField,只想将网格绑定到DataReader。以下代码帮助我在这种情况下格式化DateTime。
protected void Page_Load(object sender, EventArgs e)
{
grid.RowDataBound += grid_RowDataBound;
// Your DB access code here...
// grid.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection);
// grid.DataBind();
}
void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;
var dt = (e.Row.DataItem as DbDataRecord).GetDateTime(4);
e.Row.Cells[4].Text = dt.ToString("dd.MM.yyyy");
}
The results shown here.
结果显示在这里。
#1
77
var day = value.Date; // a DateTime that will just be whole days
var time = value.TimeOfDay; // a TimeSpan that is the duration into the day
#2
27
You can also use DateTime.Now.ToString("yyyy-MM-dd")
for the date, and DateTime.Now.ToString("hh:mm:ss")
for the time.
您还可以使用DateTime.Now.ToString(“yyyy-MM-dd”)作为日期,使用DateTime.Now.ToString(“hh:mm:ss”)作为时间。
#3
16
You can use Instance.ToShortDateString() for the date,
and Instance.ToShortTimeString() for the time to get date and time from the same instance.
您可以使用Instance.ToShortDateString()作为日期,使用Instance.ToShortTimeString()获取来自同一实例的日期和时间。
#4
1
var currentDateTime = dateTime.Now();
var date=currentDateTime.Date;
#5
-1
Sometimes you want to have your GridView as simple as:
有时您希望GridView简单如下:
<asp:GridView ID="grid" runat="server" />
You don't want to specify any BoundField, you just want to bind your grid to DataReader. The following code helped me to format DateTime in this situation.
您不想指定任何BoundField,只想将网格绑定到DataReader。以下代码帮助我在这种情况下格式化DateTime。
protected void Page_Load(object sender, EventArgs e)
{
grid.RowDataBound += grid_RowDataBound;
// Your DB access code here...
// grid.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection);
// grid.DataBind();
}
void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;
var dt = (e.Row.DataItem as DbDataRecord).GetDateTime(4);
e.Row.Cells[4].Text = dt.ToString("dd.MM.yyyy");
}
The results shown here.
结果显示在这里。