locked
What is the argument to just show date in AsDateTime() conversion? RRS feed

  • Question

  • User1595241433 posted

    Hello,

    In the following example from the book, Beginning ASP.NET Web Pages with WebMatrix, a string is converted to Date Time type.

    var x = "2010.9.3";

    var y = x.AsDateTime();

    This shows; 9/3/2010 12:00:00 AM

    How do I only show the date?

    Thanks,

    Ed

    Monday, June 4, 2012 12:03 PM

Answers

  • User1773224436 posted

    string x = "9/3/2010 12:00:00 AM";
            DateTime  y = Convert.ToDateTime(x);
            string reulst = y.ToShortDateString();

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Wednesday, June 6, 2012 4:25 AM

All replies

  • User1876843787 posted

    for coding question, maybe you can post on this link

    http://forums.asp.net/1224.aspx/1?WebMatrix

    Monday, June 4, 2012 2:04 PM
  • User-76669496 posted

    If you only want the date then try this:

    DateTime thisDate = new DateTime(2008, 3, 15);
    Console.WriteLine(thisDate.ToString("d"));

    http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

    Monday, June 4, 2012 2:10 PM
  • User1595241433 posted

    Thanks for your reply,

    I want to work with the conversion referenced above. The idea, as depicted in the book, is that you recieve a string variable and convert it to a date. In the example you provided, you already started with a date and converted it to a string.

    I tried the "d" in the argument and received an error. Maybe I didn't have the right number of ()? I'll check it out.

    Thanks, Ed

    Monday, June 4, 2012 2:58 PM
  • User1595241433 posted
    Using ("d") gives me an invalid argument. Ed
    Monday, June 4, 2012 3:01 PM
  • User1773224436 posted

    string x = "9/3/2010 12:00:00 AM";
            DateTime  y = Convert.ToDateTime(x);
            string reulst = y.ToShortDateString();

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Wednesday, June 6, 2012 4:25 AM