![](/img/trans.png)
[英]How do I convert a JSON DateTime format to a C# DateTime format from an API call
[英]How do I format a DateTime? in c#?
我有一个DateTime吗? 领域。 当前存储的UT。
我希望它以我提供的格式存储时间。 在这种情况下:
"dd.MM.yyyy HH:mm:ss"
如何更改DateTime的值? 字段来保存格式化的值。
您无需更改时间的存储方式,而可以使用将DateTime转换为字符串的方法(在您的情况下为theDate.Value.ToString(“ dd.MM.yyyy HH:mm:ss”))来更改时间表示。 )。
如果您想要始终产生指定格式的内容,则可以编写自己的日期包装器类。
string StringValue = MyDate.HasValue ? MyDate.Value.ToString("dd.MM.yyyy HH:mm:ss") : string.Empty;
请参阅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();
}
}
注意ShowFormatting方法中的strFormats数组。 该数组包含可以在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的第二个参数如何,结果都是相同的。 (您也可以定义自己的格式。)
啊! DateTime
问题...每个人都喜欢! ;-)
与该问题唯一相关的一点是,正如注释中提到的@ x2一样,DateTime(可为null或其他形式)存储为多个刻度。 您在“监视”窗口中看到的只是该值到人类可读的日期和时间部分的抽象。
为什么要存储格式化的DateTime对象? 那会不会破坏拥有DateTime变量的目的? 按照定义,“格式化”是指转换为字符串。 这意味着将创建一个单独的字符串变量,其中包含根据您的要求设置格式的Datetime值。 在其中的一条评论中,您说您需要使用此变量...为什么不从此源DateTime变量创建格式化的字符串变量。
你是这个意思吗?
string s = myDate.ToString("dd.MM.yyyy HH:mm:ss");
或可为空的DateTime:
string s = myDate == null ? null : myDate.Value.ToString("dd.MM.yyyy HH:mm:ss");
将其存储为字符串。
您不能更改日期本身的值,但是可以更改用于将日期转换为字符串的格式。
尝试以下命令:
String.Format("{0:d/M/yyyy HH:mm:ss}", dt);
很容易。 将日期保存在字符串中,如下所示:
字符串日期= DateTime.Now.Date; date.ToString(“ dd.MM.yyyy HH:mm:ss”);
我将添加我一个到列表中,我使用的参考,这是短,我可以得到它:
--- 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.