DateTime format to SQL format using C#

Blog-35.jpg

To convert the date and time from normal format to SQL standard format (YYYY-MM-DD HH:MM: SS) using C#, we have to use the ToString() function. In this blog, we will learn how we can do the above operation in more detail:

Table of Contents:

C# Program to convert DateTime format to SQL format

Let’s have a look at the program below, and how we convert DateTime Format to SQL format in C#:

Syntax:

string sqlFormattedDate = currentDateTime.ToString("yyyy-MM-dd HH:mm:ss");

Program:

Sql

Output:

DateTime in Normal format: 01/24/2025 10:52:51
DateTime in SQL format: 2025-01-24 10:52:51

Explanation: With the help of the ToString() function, we can convert the DateTime Format to SQL format.

Conclusion

So far in this article, we have learned how to convert the DateTime Format to SQ format using C#.  C# is a modern, object-oriented programming language designed specifically for the .NET framework.

Discover our collection of insightful SQL blogs that will help you master the art of managing and querying your databases efficiently.

About the Author

Technical Writer | Business Analyst

Yash Vardhan Gupta is an expert in data and business analysis, skilled at turning complex data into clear and actionable insights. He works with tools like Power BI, Tableau, SQL, and Markdown to develop effective documentation, including SRS and BRD. He helps teams interpret data, make informed decisions, and drive better business outcomes. He is also passionate about sharing his expertise in a simple and understandable way to help others learn and apply it effectively.

Intellipaat