String date format

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

This is my code, but it doesn't seem to work 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; }
    }

      

+2


a source to share


2 answers


As Oleg said, you can try to parse your value to DateTime and then format it (use try catch if necessary). This should work (not 100%, since I don't know what type of element).

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

+3


a source


Just check the type of the Value property. The above string formatter works for the System.DateTime structure. I assume in your case an object of type string. According to a given date date string, I wrote this code. Try it.

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.

0


a source







All Articles