Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am declaring a date and then passing it to an input box. I'm trying to only show the date without the time but I can't seem to get rid of the time component. Also, once I've solved this, will I be able to save the data as date only, or will having the field type as "datetime" end up with the computer adding midnight seconds?

Calling method:

C#
DateTime value = DateTime.Now.Date;
if (InputBoxes.InputBoxDate("Enter invoice date", "Enter the date for this invoice.", ref value) == DialogResult.OK)


Receiving method with all other info removed

public static DialogResult InputBoxDate(string title, string promptText, ref DateTime value)

textBox.Text = value.Date.ToString();
value = Convert.ToDateTime(textBox.Text);
return dialogResult;
Posted

1 solution

I think there is no Date only data type in C#. Hence, even if only Date is required the DateTime Type of C# is to be used, which stores both the Date and Time.

However, the the DateTime in UI controls and reports can be represented as a string in the required format using either the Custom Date and Time Format Strings[^] or using Standard Date and Time Format Strings[^]

The DateTime format is influenced by the CurrentCulture if a specific culture is not given while parsing from a string and while converting to the string format.

So, if a Culture independent conversion is required then InvariantCulture can be used as follows.

C#
DateTime today = DateTime.Now;
	
string todayAsText = today.ToString("dd/MM/yyyy",
							System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine (todayAsText);

DateTime parsedDate = DateTime.ParseExact("14/06/2012","d/M/yyyy",
							System.Globalization.CultureInfo.InvariantCulture);
//Note: d, M in the above format reads single and double digit
//date and month like 05, 5  whereas dd, MM reads only 05
Console.WriteLine (parsedDate.ToString());

/*
Output
14/06/2012
6/14/2012 12:00 
*/

So, in the above example, the code
C#
textBox.Text = value.Date.ToString();
value = Convert.ToDateTime(textBox.Text);

can be modified using DateTimeFormat as shown in the above example to achieve the effect of only Date
 
Share this answer
 
v3
Comments
Stu Baby 13-Jun-12 20:47pm    
Thanks for the explanation and the link. From the link I fathomed that all I needed to do was change
textBox.Text = value.Date.ToString();
to
textBox.Text = value.Date.ToString("d");

The data is then saved with the midnight seconds, which if I assume is how it will always be, I can then prepare for.
Once again, thanks for your input
VJ Reddy 13-Jun-12 21:08pm    
You're welcome and thank for viewing and accepting the solution :)
Tim Corey 13-Jun-12 21:57pm    
Nicely answered.
VJ Reddy 13-Jun-12 22:39pm    
Thank you, Tim :)
Vani Kulkarni 13-Jun-12 23:51pm    
My 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900