Unnikrishnan Unnikrishnan - 1 month ago 8
C# Question

newDateTime and new DateTime difference, if any

In India, the common date format is

dd/MM/yyyy
. So I input into the
TextBox
in Indian format and with the code posted in one of Stack Overflow sites, I am converting
textBox1.Text
to the format of
yyyy/MM/dd
format to further management of the string to find difference between two dates.

Though this code taken from Stack Overflow works fine with my needs, I am at a loss to understand the difference between
new DateTime
and
newDateTime
. Would anyone of you please explain? I appreciate the help.

DateTime d1;
if (DateTime.TryParse(textBox1.Text, out d1))
{
string newDateTime = d1.ToString("yyyy/MM/dd hh:mmtt");
MessageBox.Show(newDateTime.ToString());
}

Ben Ben
Answer

The DateTime is a constructor that (in this case) attempts to create a specified date format from a string. In it's simplest structure, it is declared like this:

DateTime d1 = new DateTime (int, int, int);

You then have a string which is a variable, with the name newDateTime. This is simply a string, which holds a collection of characters.

You can format a string, as you have done in your example, which would rely on the data being passed in to have some form of structure. However, once the information has been passed in, it is just a string.