TryParseExact到SQL日期格式识别



我有一个对asp.net方法的jquery请求它提供2个日期字符串参数:

qltyStartDT = "Tue Oct 30 07:00:00 PDT 2012"
qltyEndDT = "Mon Nov 12 16:00:59 PST 2012"

我正在尝试转换为sql就绪日期格式正在使用:

DateTime.TryParseExact(qltyStartDT, "", CultureInfo.InvariantCulture, DateTimeStyles.None, out QSDT);            
DateTime.TryParseExact(qltyEndDT, "", CultureInfo.InstalledUICulture, DateTimeStyles.None, out QEDT);

想弄清楚我的约会模式应该是什么?我想处理这个is>net,但如果不可行,我可以尝试用JavaScript解析。

如果日期字符串始终是EXACT格式(即28个字符的字符串(,则可以将其传递给此函数并返回日期。

VB.NET

Private Function GetDate(ByVal sDateString As String) As Date
    sDateString = sDateString.Substring(8, 2) & " " & sDateString.Substring(4, 3) & " " & sDateString.Substring(24, 4) & " " & sDateString.Substring(11, 8)
    Return Date.Parse(sDateString)
End Function

C#

private static DateTime GetDate(string sDateString)
    {
        sDateString = sDateString.Substring(8, 2) + " " + sDateString.Substring(4, 3) + " " + sDateString.Substring(24, 4) + " " + sDateString.Substring(11, 8);
        return DateTime.Parse(sDateString);
    }

这会忽略时区。

OK,所以无法使TryParseExact工作,所以我将Javascript中的日期字符串重新格式化为:(SQL模式("MM/DD/YYYY hhh:mm:ss">

JS:(注意自定义库(stackOverflow自定义库代码:自定义库

function ParseDates(chartDate) { 
         var newDate = new Date(chartDate);
         //Folowing line requires implementation of custom code
         var custFormat = newDate.customFormat("#MM#/#DD#/#YYYY# #hhh#:#mm#:#ss#"); //("#YYYY#-#MM#-#DD#");
          return custFormat; 
    }

qltyStartDT="MM/DD/YYYY hhh:MM:ss">

并使用C#:

DateTime QSDT = DateTime.Parse(qltyStartDT)

最新更新