C# DateTime changing to another format

好久不见. 提交于 2020-01-06 07:02:10

问题


Sir/Madam,

I am going to perform Oracle bulk insert but invalid bind parameter Papameters : System.dateTime warns here.

The field of Created Date is timestamp(0) which 22-MAR-13 08.13.27.000000000 PM is only accpeted format.

but when I trying to convert from string to DateTime as follows:

3/22/2013 8:00:00PM

using the following method:

 item.CreatedDate = Convert.ToDateTime("19-MAR-13 08.13.27 PM");

// BELOW IS ORACLE BULK INSERT

        using (OracleConnection myConnection = new OracleConnection(myConnectionString))
        {
            myConnection.Open();
            using (var copy = new OracleBulkCopy(myConnection))
            {
                copy.DestinationTableName = "T_BQ";
                copy.BulkCopyTimeout = 10;
                copy.BatchSize = 1000;
                var query = from item in list select item;
                var dt = new System.Data.DataTable();
                dt = ConvertToDataTable(query);
                copy.WriteToServer(dt);
                copy.Dispose();
                copy.Close();
            }
            myConnection.Dispose();
            myConnection.Close();
        }

回答1:


You can use DateTime.TryParseExact for custom datetime format as:

 string strDateStarted = "19-MAR-13 08.13.27 AM";
 DateTime datDateStarted;
 DateTime.TryParseExact(strDateStarted, new string[] { "dd-MMM-yy hh.mm.ss tt" }, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out datDateStarted);
 Console.WriteLine(datDateStarted);



回答2:


You should using : for time separator instead .

Try this:

item.CreatedDate = Convert.ToDateTime("19-MAR-13 08:13:27 PM");



回答3:


try this,

dateVariable.ToString("MM/dd/yyyy hh:mm:sstt");


来源:https://stackoverflow.com/questions/15606925/c-sharp-datetime-changing-to-another-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!