• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 460
  • Last Modified:

How do you format a DateTime variable...

I have a DateTime variable that is set to this: "rentDate = DateTime.Now.Date;"
This gets the correct date i need, however it also attaches the time to it.  How do i format the variable so that it is only in mm/dd/yyyy so that I can use it to query an access database for an instance of that date.  I also have a dateTimePicker that I am selecting the current date for the query.

I am attaching a txt file with the code this variable is implemented in and the code it interacts with.
Code.txt
0
udeeze
Asked:
udeeze
  • 3
  • 2
  • 2
  • +1
4 Solutions
 
Luis PérezSoftware Architect in .NetCommented:
If you want to remove the time portion from a DateTime variable, you can use this function:

public DateTime RemoveTime(DateTime theDate)
{
    return new DateTime(theDate.Year, theDate.Month, theDate.Day);
}

This will create a new DateTime variable from the Year, Month and Day parts of your original variable, and deprecating the time portion.

Hope that helps.
0
 
udeezeAuthor Commented:
This did not work, I used the exact same code as you have here and the date still attaches the time portion of it.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Instead of "DateTime", use "Date"....
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
drunk_irishmanCommented:
this is a function i put in all my global class to format my dates.

using this you would just pass datestring(DateTime.Now.ToString())

or you could do something like:

DateTime.Parse(DateTime.Now.ToString()).ToString("d");
public static string datestring(string dstr)
        {
            if (dstr == "")
            {
                return dstr;
            }
            else
            {
                return DateTime.Parse(dstr).ToString("d");
            }
        }

Open in new window

0
 
drunk_irishmanCommented:
sorry, in your case it would be:

rentDate = DateTime.Parse(DateTime.Now.ToString()).ToString("d");
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
@drunk_irishman: Why convert DateTime.Now() to String, Parse it back to a DateTime, then format it as a String again?...

"rentDate" is ALREADY a DateTime, so just use ToString() on it:

    string strRentDate = rentDate.ToString("d");

Or go directly there:

    string strRentDate = DateTime.Today.ToString("d");
0
 
drunk_irishmanCommented:
Sorry misunderstood that rentDate was a DateTime variable.  
0
 
udeezeAuthor Commented:
Thank you for your help.  I ended up using
DateTime.Today.Date.ToShortDateString()
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now