Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is the way to convert 'mm/dd/yyyy' to SQL Server DateTime Format using C#..?

user-image
Question added by Javed Ahmad , Senior Developer , HCL Technologies Ltd
Date Posted: 2016/09/01
Pathan Zarrar Khadar Khan
by Pathan Zarrar Khadar Khan , Windows System Administrator , Dyce Tech Pvt Ltd

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

Shahid Salam Khan
by Shahid Salam Khan , Software Engineer , Xavient Information Systems

could you please explain it,

as per my understanding if you have mm/dd/yyy date with data type string then you just need to convert it to c# datetime data type and then just pass it through your procedure or yourquery

Noorulla Khan
by Noorulla Khan , Software Developer , Ixtel Technologies

Simply create a custom method like this

 

/// <summary>

        /// Converts string like "mm/dd/yyyy hh:mm" to Sql Date format

        /// </summary>

        /// <param name="date"></param>

        /// <returns></returns>

        public static DateTime ConvertStringtoDateTime(string date)

        {

            string sqldateformat = String.Format("{0}-{1}-{2} {3}", date.Substring(6, 4), date.Substring(3, 2), date.Substring(0, 2), date.Substring(11, 5));

            DateTime dt = Convert.ToDateTime(sqldateformat);

            return dt;

        }

Raghav Mehra
by Raghav Mehra , Team Lead - Software Engineer , Mobiquest Mobile Technologies

This can be easily achieved by 

 

 

date.ToString("MM/dd/yyyy");

Fraz Ahmed
by Fraz Ahmed , Software Developer , FACTS Computer Software House LLC

Easiest way would be using the inbuilt ToString function

DateTime myDateTime =DateTime.Now;string sqlFormattedDate = myDateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");

More Questions Like This