-1

我想将以下日期和时间格式转换为 SQL 日期和时间 Thu Apr 07 2016 06:30:00 GMT+0530 (India Standard Time) 任何人都知道谢谢。

4

2 回答 2

1

由于您的字符串具有UTC偏移值,因此我会将其解析为,DateTimeOffset而不是DateTime因为它可以保存偏移部分。

但是既不DateTime也不DateTimeOffset保留时区信息,您应该使用GMTand(India Standard Time)部分作为字符串文字分隔符。

var s = "Thu Apr 07 2016 06:30:00 GMT+0530 (India Standard Time)";
var dto = DateTimeOffset.ParseExact(s, "ddd MMM dd yyyy HH:mm:ss 'GMT'zzz '(India Standard Time)'", 
                                    CultureInfo.InvariantCulture);

现在你有一个DateTimeOffsetas {07.04.2016 06:30:00 +05:30}

在此处输入图像描述

我会将它dto作为datetimeoffset类型列插入到 SQL Server 中(当然还有参数化查询),因为它也保存了偏移量部分。

+---------------------------+
|   Time zone offset range  |
+---------------------------+
|   -14:00 through +14:00   |
+---------------------------+
于 2016-04-07T13:47:58.090 回答
-1

如果您的输入是一个字符串,您将需要开始解析特定文化的日期:

DateTime dt =  DateTime.ParseExact(inputString, System.Globalization.CultureInfo("<your_culture>"));

<your_culture>您所在国家/地区的多种文化名称之一在哪里(请参阅http://www.csharp-examples.net/culture-names/

然后您可以将日期作为与 SQL 兼容的字符串返回,其中包含简单的引号:

string sqlDate = dt.ToString("'yyyy-MM-dd HH:mm:ss'");
于 2016-04-07T13:36:51.083 回答