views:

158

answers:

3

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.

+1  A: 

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 );
tvanfosson
I tried your ParseExact suggestion and I get the same error as what I posted in Joel Coehoorn's comment section.
Refracted Paladin
Is the last component of the date using a colon or period separator? You might have to adjust my format string if it's using a period separator. Your question showed a colon so I used that, but typically it would be a period for the fractional seconds.
tvanfosson
The period was the issue. I got it now using a combination of your ParseExact suggestion and Joel's Property suggestion. Since he has more votes I guess you get the credit. Would be kind of nice to mark multiple answers, sometimes.
Refracted Paladin
+4  A: 
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.

Joel Coehoorn
I think my problem is that the Format of the String return by LastUpdatedTime is weird --> `20090727 10:45:49.990` and it errors out saying "String was not recognized as a Valid DateTime"
Refracted Paladin
A: 

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);
}
Daniel Brückner