Let us explore the various examples and usage of the DateTime.Parse method.
Example 1: Basic Usage
The following example demonstrates how to use this function to parse a given date time into a DateTime object:
The previous code should convert the value into a DateTime object as shown in the output:
Example 2: Parsing the Date with Time
If you do not provide the time value, the function uses 0000hrs as the default value. However, you can provide a value as follows:
In this case, the date string includes the provided date value in the output as follows:
Example 3: Specifying the Timezone Info
If we pass the Timezone information, the method automatically parses the value as shown in the following example:
In the previous example, we specify the Timezone with the hour difference. This allows the “Parse” method to adjust the resulting DateTime object to reflect the changes.
An example is as follows:
Example 4: Using the Custom Format Specifiers
We can also specify a custom date and time specifier which allows the method to format the resulting DateTime object into the resulting format.
An example is as follows:
The resulting output is as follows:
Conclusion
This short tutorial describes how to use the “Parse” method to parse the date and time values into the DateTime objects. Feel free to explore the docs for more details.