[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 453
  • 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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
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

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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