设置字符串格式以正确显示日期
我需要帮助将此字符串 --> 20090727 10:16:36:643
转换为 --> 07/27/2009 10:16:36
原始日期和时间由 SynchronizationAgent.LastUpdated()
函数返回,其中返回上述格式的字符串。
原始问题:保留供参考
我有这个 -->
HUD.LastSyncDate = mergeSubscription.SynchronizationAgent.LastUpdatedTime;
它正在设置一个看起来像这样的属性 -->
public static string LastSyncDate
{
get { return _lastSyncDate; }
set
{
_lastSyncDate = String.Format(CultureInfo.InvariantCulture,"{0:G}", value);
}
}
不幸的是,有或没有 String .Format
显示的日期看起来像这样 --> 20090727 10:16:36:643
我尝试了多种变体来格式化它我想。 我缺少什么?
根据以下建议(主要是 Joel 的),我实现了建议的更改,但我仍然收到 “String is not a valid DateTime error”
我也尝试实现此 - ->
HUD.LastSyncDate = DateTime.ParseExact(mergeSubscription.SynchronizationAgent.LastUpdatedTime,"yyyyMMdd HH:mm:ss:fff",CultureInfo.InvariantCulture);
但还是什么都没有。
I need help converting this string --> 20090727 10:16:36:643
to --> 07/27/2009 10:16:36
The original date and time are being returned by the SynchronizationAgent.LastUpdated()
function, which returns a String in the above format.
Original question:preserved for reference
I have this -->
HUD.LastSyncDate = mergeSubscription.SynchronizationAgent.LastUpdatedTime;
Which is setting a property that looks like this -->
public static string LastSyncDate
{
get { return _lastSyncDate; }
set
{
_lastSyncDate = String.Format(CultureInfo.InvariantCulture,"{0:G}", value);
}
}
Unfortunately, with or without the String.Format
the date that is displayed looks like this --> 20090727 10:16:36:643
I have tried multiple variations to Format it the way I want. What am I missing?
Based on the below suggestions(Mostly Joel's), I implemented the suggested changes but I am still getting a "String is not a valid DateTime error"
I also tried implementing this -->
HUD.LastSyncDate = DateTime.ParseExact(mergeSubscription.SynchronizationAgent.LastUpdatedTime,"yyyyMMdd HH:mm:ss:fff",CultureInfo.InvariantCulture);
but still nothing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在此处放置任何您想要的格式字符串。 但听起来您真正想要的更像是这样:
将其保留为后台的日期时间,并仅使用字符串属性进行显示。
You can put any format string you want there. But it sounds like what you really want is something more like this:
Keep it as a datetime in the background and just use the string property for display.
在我看来,LastUpdatedTime 实际上是一个字符串(因为您可以进行分配)而不是 DateTime。 在这种情况下,应用的格式不会执行任何操作。 您需要将 LastUpdatedTime 解析为 DateTime,然后将其重新格式化为您想要的格式,然后再将其分配给字符串。
请注意,您可能需要使用 ParseExact 来代替。
It appears to me that LastUpdatedTime is actually a string (since you can do the assignment) not a DateTime. In that case, the format applied won't do anything. You'll want to parse the LastUpdatedTime into a DateTime then reformat into the format that you want before assigning it to your string.
Note that you may need to use ParseExact instead.
你想让我做什么? 您获得一个字符串,将其传递给
String.Format()
并将其存储在字符串字段中。 您想重新格式化字符串吗? 在这种情况下,您必须将字符串解析回DateTime
并再次格式化该值。What do you want to do? You get a string, pass it to
String.Format()
and store it in a string field. Do you want to reformat the string? In this case you have to parse the string back toDateTime
and format this value again.