C#DateTime更改为另一种格式 [英] C# DateTime changing to another format

查看:182
本文介绍了C#DateTime更改为另一种格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生/女士,

我将执行Oracle批量插入,但是绑定参数Papameters无效:System.dateTime在此处警告.

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

创建日期"字段为timestamp(0),其中13-MAR-13 08.13.27.000000000 PM仅采用格式.

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

但是当我尝试从字符串转换为DateTime时,如下所示:

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

3/22/2013 8:00:00 PM

3/22/2013 8:00:00PM

使用以下方法:

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

//下面是Oracle批量插入

// 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();
        }

推荐答案

您可以使用 DateTime.TryParseExact ,用于自定义datetime格式,为:

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);

这篇关于C#DateTime更改为另一种格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆