我有一个DateTime?领域。它目前正在存储UT。如何格式化日期时间?在C#中?
我希望它能以我提供的格式存储时间。在这种情况下:
"dd.MM.yyyy HH:mm:ss"
如何更改DateTime的值?字段来保存格式化的值。
我有一个DateTime?领域。它目前正在存储UT。如何格式化日期时间?在C#中?
我希望它能以我提供的格式存储时间。在这种情况下:
"dd.MM.yyyy HH:mm:ss"
如何更改DateTime的值?字段来保存格式化的值。
string StringValue = MyDate.HasValue ? MyDate.Value.ToString("dd.MM.yyyy HH:mm:ss") : string.Empty;
你的意思是这个:?
string s = myDate.ToString("dd.MM.yyyy HH:mm:ss");
或可空日期时间:
string s = myDate == null ? null : myDate.Value.ToString("dd.MM.yyyy HH:mm:ss");
商店作为字符串。
无法更改其来自Web服务的数据类型...如果可能确实需要使用此DateTime?数据类型... – 2009-09-09 07:40:17
@JL,实际的DateTime存储为一个很长的滴答数。你看到的只是格式化字符串,所以你也必须格式化它。 – 2009-09-09 07:43:40
您不会改变时间的存储方式,您可以通过使用将DateTime转换为字符串的方法来更改时间表示形式(在您的情况下,theDate.Value.ToString(“dd.MM.yyyy HH:mm: SS“))。
如果您想要某种始终产生您指定格式的东西,则可以编写自己的日期包装类。
很好的答案,特别是关于存储的评论! +1 – Cerebrus 2009-09-09 07:49:56
我不认为OP的意思是'存储',因为这个价值来自于网络服务。我认为他意味着如何显示该字段中的DateTime(DataView也许)! – stevehipwell 2009-09-09 08:00:50
您不能更改日期本身的值,但可以改变用于将其转换为字符串的格式。
试试下面的命令:
String.Format("{0:d/M/yyyy HH:mm:ss}", dt);
非常容易。将日期保存为字符串,如下所示:
String date = DateTime.Now.Date; date.ToString(“dd.MM.yyyy HH:mm:ss”);
啊! DateTime
问题...大家的最爱! ;-)
这个问题唯一相关的一点是,作为@ x2在评论中提到,一个DateTime(可为空或以其他方式)存储为多个刻度。您在Watch窗口中看到的只是将该值抽象为人类可读的日期和时间组件。
为什么要存储格式化的DateTime对象?这不会打败拥有DateTime变量的目的吗? “格式化”,根据定义意味着转换为字符串。这意味着创建一个单独的字符串变量,该变量保存按照您的要求格式化的Datetime值。在其中一个注释中,你说你需要使用这个变量...为什么不从这个源DateTime变量创建一个格式化的字符串变量,而是。
见本expample日期时间格式在C#
using System;
using System.Globalization;
class DateAndTimeFormatting
{
static DateTime dt = DateTime.Now;
static void Main()
{
ShowFormatting(DateTimeFormatInfo.InvariantInfo, "InvariantInfo");
ShowFormatting(DateTimeFormatInfo.CurrentInfo, "CurrentInfo");
}
static void ShowFormatting(DateTimeFormatInfo format, string strLabel)
{
Console.WriteLine(strLabel);
Console.WriteLine(new string('-', strLabel.Length));
string[] strFormats = {"d", "D", "f", "F", "g", "G", "m", "r", "s", "t", "T", "u", "U", "y" };
foreach (string strFormat in strFormats)
Console.WriteLine("{0}: {1}", strFormat, dt.ToString(strFormat, format));
Console.WriteLine();
}
}
通知的strFormats阵列中的ShowFormatting方法。该数组包含可以在ToString方法中使用的格式化字符串。 (可以使用在占位符的那些相同的字母在Console.WriteLine的可格式化字符串。)该程序首先示出了用于DateTimeFormatInfo.InvariantInfo格式化:
InvariantInfo
d: 12/02/2006
D: Saturday, 02 December 2006
f: Saturday, 02 December 2006 16:48
F: Saturday, 02 December 2006 16:48:43
g: 12/02/2006 16:48
G: 12/02/2006 16:48:43
m: December 02
r: Sat, 02 Dec 2006 16:48:43 GMT
s: 2006-12-02T16:48:43
t: 16:48
T: 16:48:43
u: 2006-12-02 16:48:43Z
U: Saturday, 02 December 2006 21:48:43
y: 2006 December
以下格式示出了用于DateTimeFormatInfo.CurrentInfo:
CurrentInfo
d: 12/2/2006
D: Saturday, December 02, 2006
f: Saturday, December 02, 2006 4:48 PM
F: Saturday, December 02, 2006 4:48:43 PM
g: 12/2/2006 4:48 PM
G: 12/2/2006 4:48:43 PM
m: December 02
r: Sat, 02 Dec 2006 16:48:43 GMT
s: 2006-12-02T16:48:43
t: 4:48 PM
T: 4:48:43 PM
u: 2006-12-02 16:48:43Z
U: Saturday, December 02, 2006 9:48:43 PM
y: December, 2006
当大写和小写字母产生不同的结果(如d和D)时,大写字母会生成更长的字符串。对于r,R,s或u格式的字符串,无论ToString的第二个参数如何,结果都是相同的。 (您也可以定义自己的格式。)
这样的事情很有用 - http://john-sheehan.com/blog/wp-content/uploads/msnet-formatting-strings.pdf – RichardOD 2009-09-09 08:13:52
我会添加my one到列表中,我使用的参考,这是短,我可以得到它:
--- Default formats ---
Tokens:
dD tT fF gG rsu M Y
d = short/long [d]ate
t = short/long [t]ime
f = short/long [f]ull
g = short/long [g]eneral or default
rsu = RFC or ISO format, last 2 are sortable:
r: Fri, 20 Mar 2009 00:00:00 GMT (RFC1123)
s: 2009-03-20T00:00:00 (ISO 8601)
u: 2009-03-20 00:00:00Z (RFC1123)
M = day & month
Y = month & year
--- Custom formats ---
dd ddd dddd
15 Mon Monday
MM MMM MMMM
06 Jun June
yy yyyy
08 2008
hh HH
01 13
mm
ss
tt
AM or PM
你是什么意思“存储“ - >在哪里? DB,字符串,文本......例如除非在系统设置中更改,否则不会更改数据库使用的表示形式。 – 2009-09-09 07:38:13
什么是日期时间?和它是如何不同于正常日期时间 – 2009-09-09 07:44:17
@incredible_Honk - 这是一个nullabe日期时间 – stevehipwell 2009-09-09 07:46:08