Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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

user-image
Question ajoutée par Javed Ahmad , Senior Developer , HCL Technologies Ltd
Date de publication: 2016/09/01
Pathan Zarrar Khadar Khan
par 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
par 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
par 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
par Raghav Mehra , Team Lead - Software Engineer , Mobiquest Mobile Technologies

This can be easily achieved by 

 

 

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

Fraz Ahmed
par 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