
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
DateTime.ToString() Method in C#
The DateTime.ToString() method in C# is used to convert the value of the current DateTime object to its equivalent string representation.
Syntax
Following is the syntax −
ToString(String, IFormatProvider) ToString() ToString(IFormatProvider) ToString(String)
Example
Let us now see an example to implement the DateTime.ToString() method −
using System; public class Demo { public static void Main() { DateTime d = new DateTime(2019, 11, 11, 7, 11, 25); Console.WriteLine("Date = {0}", d); string str = d.ToString(); Console.WriteLine("String representation = {0}", str); } }
Output
This will produce the following output −
Date = 11/11/2019 7:11:25 AM String representation = 11/11/2019 7:11:25 AM
Example
Let us now see another example to implement the DateTime.ToString(String) method −
using System; public class Demo { public static void Main() { DateTime d = new DateTime(2019, 11, 11, 7, 11, 25); Console.WriteLine("Date = {0}", d); string str = d.ToString("f"); Console.WriteLine("String representation = {0}", str); } }
Output
This will produce the following output −
Date = 11/11/2019 7:11:25 AM String representation = Monday, November 11, 2019 7:11 AM
Advertisements