简体   繁体   English

将字符串转换为日期没有时间

[英]convert string to date without time

This line 这条线

System.DateTime.Parse("09/12/2009"); 

convert date (string) to 9/12/2009 12:00:00 AM. 将日期(字符串)转换为9/12/2009 12:00:00 AM。 How can I get a date in the form 9/12/2009. 我怎样才能在9/12/2009的表格中找到日期。

after explanations I do: 在解释之后我做了:

DateTime dt = System.DateTime.Parse(Request.Form["datepicker"]);
dt.ToString("dd/mm/yyyy");
/* and this code have time, why???*/

Your problem is not with the parsing but with the output. 你的问题不是解析,而是输出。 Look at how ToString works for DateTime, or use this example: 看看ToString如何为DateTime工作,或者使用这个例子:

using System;

class Program
{
    static void Main(string[] args)
    {
        DateTime dt = DateTime.Parse("09/12/2009");
        Console.WriteLine(dt.ToString("dd/MM/yyyy"));
    }
}

Or to get something in your locale: 或者在您的语言环境中获取内容:

Console.WriteLine(dt.ToShortDateString());

Update: Your update to the question implies that you do not understand fully my answer yet, so I'll add a little more explanation. 更新:您对问题的更新意味着您还没有完全理解我的答案,所以我会再添加一些解释。 There is no Date in .NET - there is only DateTime. .NET中没有日期 - 只有DateTime。 If you want to represent a date in .NET, you do so by storing the time midnight at the start of that day. 如果要在.NET中表示日期,可以通过存储当天开始的午夜时间来完成。 The time must always be stored, even if you don't need it. 即使您不需要,也必须始终存储时间。 You cannot remove it. 你无法删除它。 The important point is that when you display this DateTime to a user, you only show them the Date part. 重要的一点是,当您向用户显示此DateTime时,只显示日期部分。

Anything parsed to a DateTime object will contain date+time. 解析为DateTime对象的任何内容都将包含日期+时间。 If no time is sepcified, the assumed value will be 0000hr. 如果没有时间,则假定值为0000hr。

To get the date-only representation, it's up to how you format it to string. 要获取仅限日期的表示形式,您可以将其格式化为字符串。

Eg theDate.ToString("dd/MM/yyyy") 例如theDate.ToString("dd/MM/yyyy")

Refer to the MSDN Date and Time Format Strings . 请参阅MSDN 日期和时间格式字符串

if you want to convert gridview datetime column to date only column use this code : 如果要将gridview datetime列转换为仅日期列,请使用以下代码:

raddgvDaybook.Rows.ToList().ForEach(x =>
{
    DateTime dt = DateTime.Parse(x.Cells["Vocdate"].Value.ToString());
    string str = string.Format("{0:MM/dd/yyyy}", dt);
    x.Cells["Vocdate"].Value = str;
});

I tested the code it will work if you bind the datetime as string in dgv. 如果你将日期时间绑定为dgv中的字符串,我测试了它将起作用的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM