tags:

views:

111

answers:

2

I am using the Vimeo API and I want to convert the string <upload_date> to a short date format, {0:d} or {0:dd/mm/yyyy}.

This is my code but it doesn't seem to be working for me.

    select new VimeoVideo
            {
                Date = String.Format("{0:d}",(item.Element("upload_date").Value)),
            };
        return Vids.ToList();
    }

    public class VimeoVideo
    {
        public string Date { get; set; }
    }
+3  A: 

As Oleg suggested you can try to parse your value to DateTime and then format it (use try catch if needed). That should work (not 100% sure since I don't know what item's type is).

var myDate = DateTime.Parse(item.Element("upload_date").Value);
Date = String.Format("{0:d}", myDate);

http://msdn.microsoft.com/it-it/library/1k1skd40(v=VS.80).aspx

mamoo
This worked perfect thanks!
Phil
A: 

Just verify the type of the Value property.. The above string formatter works for System.DateTime structure.. I assume in your case its string type object. According to the given sample date time string i have written this code.. Try out this.

CultureInfo provider = CultureInfo.InvariantCulture;
var format = "yyyy-MM-dd HH:mm:ss";
var dt = DateTime.ParseExact(item.Element("upload_date").Value, format, provider);
Date = string.Format("{0:d}", dt);

Hope it works..

djrockz007