设置字符串格式以正确显示日期

发布于 2024-07-27 22:32:29 字数 1172 浏览 2 评论 0原文

我需要帮助将此字符串 --> 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笛声青案梦长安 2024-08-03 22:32:29
HUD.LastSyncDate = DateTime.Parse(mergeSubscription.SynchronizationAgent.LastUpdatedTime).ToString("MM/dd/yyyy")

您可以在此处放置任何您想要的格式字符串。 但听起来您真正想要的更像是这样:

private static DateTime _lastSyncDate;
public static DateTime LastSyncDate
{
    get { return _lastSyncDate; }
    set { _lastSyncDate = value;}
}

public static string LastSyncDateString
{
    get { return LastSyncDate.ToString("MM/dd/yyyy"); }
}

将其保留为后台的日期时间,并仅使用字符串属性进行显示。

HUD.LastSyncDate = DateTime.Parse(mergeSubscription.SynchronizationAgent.LastUpdatedTime).ToString("MM/dd/yyyy")

You can put any format string you want there. But it sounds like what you really want is something more like this:

private static DateTime _lastSyncDate;
public static DateTime LastSyncDate
{
    get { return _lastSyncDate; }
    set { _lastSyncDate = value;}
}

public static string LastSyncDateString
{
    get { return LastSyncDate.ToString("MM/dd/yyyy"); }
}

Keep it as a datetime in the background and just use the string property for display.

赏烟花じ飞满天 2024-08-03 22:32:29

在我看来,LastUpdatedTime 实际上是一个字符串(因为您可以进行分配)而不是 DateTime。 在这种情况下,应用的格式不会执行任何操作。 您需要将 LastUpdatedTime 解析为 DateTime,然后将其重新格式化为您想要的格式,然后再将其分配给字符串。

DateTime lastUpdated = DateTime.Parse( mergeSubscription.SynchronizationAgent.LastUpdatedTime );
HUD.LastSyncDate = string.Format( "{0:G}", lastUpdated );

public static string LastSyncDate { get; set; }

请注意,您可能需要使用 ParseExact 来代替。

DateTime lastUpdated = DateTime.ParseExact( "yyyyMMdd HH:mm:ss:fff",
                                            ...,
                                            CultureInfo.InvariantCulture );

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.

DateTime lastUpdated = DateTime.Parse( mergeSubscription.SynchronizationAgent.LastUpdatedTime );
HUD.LastSyncDate = string.Format( "{0:G}", lastUpdated );

public static string LastSyncDate { get; set; }

Note that you may need to use ParseExact instead.

DateTime lastUpdated = DateTime.ParseExact( "yyyyMMdd HH:mm:ss:fff",
                                            ...,
                                            CultureInfo.InvariantCulture );
风和你 2024-08-03 22:32:29

你想让我做什么? 您获得一个字符串,将其传递给 String.Format() 并将其存储在字符串字段中。 您想重新格式化字符串吗? 在这种情况下,您必须将字符串解析回 DateTime 并再次格式化该值。

DateTime dateTime;
if (DateTime.TryParse(value, out dateTime))
{
    lastSyncDate = String.Format(CultureInfo.InvariantCulture,"{0:G}", dateTime);
}
else
{
    HandleInvalidInput(value);
}

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 to DateTime and format this value again.

DateTime dateTime;
if (DateTime.TryParse(value, out dateTime))
{
    lastSyncDate = String.Format(CultureInfo.InvariantCulture,"{0:G}", dateTime);
}
else
{
    HandleInvalidInput(value);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文