Need Help with DateTime in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LoanB
    New Member
    • Nov 2007
    • 62

    Need Help with DateTime in C#

    Hi There.

    I have a variable called dtDate. I want to assign today\s date to it in short format:

    E.G.: 2007/12/07

    However I get it in the following format:

    2007/12/07 0:00:00 - It includes the time.

    How can I just get the date?

    I have tried: the dtDate.ToShortS tring() etc, but with no success.

    Thanks,

    Lóan
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    What about .ToShortDateStr ing()? If you want to rearrange the position of the year, month, date you will need to use String.Format.

    DateTime on msdn

    Nathan

    Comment

    • LoanB
      New Member
      • Nov 2007
      • 62

      #3
      Hi Nathan

      Appologies I meant .ToShortDateStr ing() not ".ToShortString ()"

      Still returns the time as well.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        DateTime objects will always store a "date and time"
        You can format the string version of it with .ToString()

        So:
        Code:
        string mydatestring="";
        mydatestring=DateTime.Now.ToString("yyyyMMdd");
        //mydatestring is now "20071207"

        Comment

        • LoanB
          New Member
          • Nov 2007
          • 62

          #5
          Hey guys

          I feel like a bit of an A*s, In my database the field name was "Dates" and in the code I had "Date" - natuarally not going to work. Hence the problem I had is now fixed.

          As my field type is DateTime in the Database I could not use ToShortDateStri ng(), us assign it to a String.

          Well all is well, and my C# knowledge is expanding

          Thanks Nathan and Plater!

          Comment

          • drutchlin
            New Member
            • Aug 2006
            • 1

            #6
            To get just todays date use DateTime.Today( )

            Comment

            • sumitgupta10
              New Member
              • Dec 2007
              • 13

              #7
              Hi,
              To get just todays date,try this one,
              DateTime.Now.To ShortDateString ()

              Comment

              Working...