C# DateTime ToString: Changing Your Variable Data Type

javadatatypesIn any programming language, you’ll find the need to change a variable’s data type. Some languages make it difficult, but C# makes it easy to switch from one data type to a string. You switch from the DateTime format to a string using the “ToString()” method. This method is an invaluable tool when you write any C# program whether it’s a console, web app or Windows service.

Take a quick fundamental C# course at Udemy.com

Creating a DateTime Variable

The DateTime structure defines several properties and methods that let you work with dates. Instead of worrying about the right format, the DateTime structure gives you the option to format your dates and times using only one method. This makes it much easier, especially when you need to work with several dates at a time. Instead of creating a custom method to format each date, you simply use the DateTime structure’s internal methods.

The following code shows you how to create a date in C#:

DateTime mydate = DateTime.Now;

The “Now” property is one of those simple properties that makes it easy to work with dates. Instead of calling the operating system’s internal DLLs and retrieving the system date and time, the “Now” property does all of that for you. In the example code above, the “mydate” variable contains the current date and time registered on the local machine.

Understand how to work with C# data types at Udemy.com

Now, what happens when you want to print the date to the screen or use it with another string? If you try to use a DateTime without converting it first to a string, your program will display an error and crash. Consider the following code:

DateTime mydate = DateTime.Now;

Console.Write(“The current date and time is: “ + mydate.ToString());

Notice the “ToString()” method in the above code. This code converts the date to a string before using it to display the current date and time to the user in the console. When you work with C#, you will notice that several lines of code use the ToString method, especially when you work with front-end GUI applications.

Other ToString Uses

You can use the ToString method with several other data types. You can use it with integers, dates, decimals and even objects.

Consider the following code:

int mynumber = 0;

The above code declares an integer value. You can only manipulate this variable with other integer values. To use it with a string (or any other data type), you need to convert it to the corresponding string data type. The following code changes the value to a string data type that contains the value “0”:

int mynumber = 0;

string myconvertednumber = mynumber.ToString();

Notice that the variables are lower case but “ToString” always uses a certain format. C# is case sensitive, so you must use the exact upper and lower case letters set up in the structure’s method. In other words, if you tried to run “mynumber.tostring(),” your compiler would throw and error and you would be forced to change the method to the proper format.

The ToString method is also available using the “Convert” class. The Convert class lets you transfer between different data types. For instance, the ToString method converts data to a string, but what if you want to convert to an integer first and then convert your variable to a string? You need the Convert class to switch between data types.

Consider the following code:

string myval = “23”;

Although a number is stored in “myval,” the quotes indicate to the compiler that the data type is a string. However, since the value is actually a number, it can be converted to the integer data type using Convert. The following code converts the variable to an integer:

string myval = “23”;

int convertedval = Convert.ToInt32(myval);

The above statement converts the string value to an integer and stores it in the convertedval variable. You can also use this same Convert class to convert a value to a date and a time. For instance, the following code creates a date and a time variable that is actually set up as a string. The code later converts the string to a date and a time (the opposite of what was created in the first example):

string mydate = “4/13/2013”;

DateTime myrealdate = Convert.ToDateTime(mydate);

The above code performs exactly the opposite of what was created using the ToString method. The value was first set up as a string and then later converted to a date. This is important when you need to alternate between two data types to manipulate dates and print them out to your GUI.

You can also use the ToString method to print out multiple values. For instance, you might have a list of numbers and need to print them out to the console. The following code is an example of this type of structure:

foreach (var number in numbers)


Console.WriteLine(“This is number “ + number.ToString());


In the above code, an enumerable item named “numbers” is looped through using the foreach statement. Although the code does not show the numbers list being created, you can assume it is a list of numbers from the code. To keep your code clean and to avoid any bugs, you convert the number to a string before you print it to the console. Each item is given the variable name “number” and this number is converted to a string before it’s printed to the console.

Strings are just one data type you will encounter when you work with the C# programming language. Learning data types is a part of any programming language, but once you understand one, you will have an easier time learning other languages. For instance, the Java language has a toString() method you can use to convert to a string. Converting between data types will be a common process when you work in the programming industry.

Go from a beginner to an advanced C# coder with a programming class at Udemy.com