2014-05-02 49 views
0

我有一個jquery請求asp.net方法 提供2日期字符串PARAMS:TryParseExact到SQL日期格式識別

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

試圖找出我的約會模式應該是什麼? 我想處理這個>網,但如果沒有可行的,我可以嘗試在JavaScript中解析出。

回答

0

如果您的日期字符串,準確的格式(即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); 
    } 

這忽略時間段。

+0

問題是我不能忽略時區,因爲它包括夏令時的轉移,即PDT - > PST。 我將訴諸手動解析日期加上或減去必要的一個小時,但我希望我不必編寫所有的邏輯,我相信它是把手的地方 – DaniDev

0

確定,所以無法得到TryParseExact在JavaScript中工作,所以我重新格式化我的日期字符串:(SQL模式) 「MM/DD/YYYY hhh:mm:ss

JS(注意:自定義庫)stackOverflow custom Library代碼:custom Library

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)