如何仅显示和存储日期而没有时间. [英] How do I only display and store the date without time.
问题描述
你好,
我声明一个日期,然后将其传递到输入框.我试图只显示日期而不显示时间,但是我似乎无法摆脱时间部分.另外,一旦解决了这个问题,我将只能将数据保存为日期,还是将字段类型设置为"datetime",最后计算机将添加午夜秒数呢?
调用方法:
Hello,
I am declaring a date and then passing it to an input box. I''m trying to only show the date without the time but I can''t seem to get rid of the time component. Also, once I''ve solved this, will I be able to save the data as date only, or will having the field type as "datetime" end up with the computer adding midnight seconds?
Calling method:
DateTime value = DateTime.Now.Date;
if (InputBoxes.InputBoxDate("Enter invoice date", "Enter the date for this invoice.", ref value) == DialogResult.OK)
删除所有其他信息的接收方法
Receiving method with all other info removed
public static DialogResult InputBoxDate(string title, string promptText, ref DateTime value)
textBox.Text = value.Date.ToString();
value = Convert.ToDateTime(textBox.Text);
return dialogResult;
推荐答案
我认为没有Date only data type in C#
.因此,即使需要only Date
,也要使用C#的DateTime
Type,它存储了两个Date and Time.
但是,可以使用 ^ ]或使用标准日期和时间格式字符串 [ ^ ]
如果从字符串解析和转换为字符串格式时未提供特定的区域性,则DateTime
格式会受CurrentCulture
的影响.
因此,如果需要独立于Culture
的转换,则可以按以下方式使用InvariantCulture
.
I think there is noDate only data type in C#
. Hence, even ifonly Date
is required theDateTime
Type of C# is to be used, which stores both theDate and Time.
However, the theDateTime
inUI controls and reports
can be represented as astring
in therequired format
using either the Custom Date and Time Format Strings[^] or using Standard Date and Time Format Strings[^]
TheDateTime
format is influenced by theCurrentCulture
if a specific culture is not given while parsing from a string and while converting to the string format.
So, if aCulture
independent conversion is required thenInvariantCulture
can be used as follows.
DateTime today = DateTime.Now;
string todayAsText = today.ToString("dd/MM/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine (todayAsText);
DateTime parsedDate = DateTime.ParseExact("14/06/2012","d/M/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
//Note: d, M in the above format reads single and double digit
//date and month like 05, 5 whereas dd, MM reads only 05
Console.WriteLine (parsedDate.ToString());
/*
Output
14/06/2012
6/14/2012 12:00
*/
因此,在上面的示例中,代码
So, in the above example, the code
textBox.Text = value.Date.ToString();
value = Convert.ToDateTime(textBox.Text);
可以使用如上例所示的DateTimeFormat
进行修改,以达到only Date
can be modified using DateTimeFormat
as shown in the above example to achieve the effect of only Date
这篇关于如何仅显示和存储日期而没有时间.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!