2009-08-18 65 views
0

我不是一個強大的ASP經典開發人員,但我的任務是在工作中支持此應用程序,以及我一直試圖轉換將RSS提要日期轉換爲短日期格式。我似乎無法找到解決方案。RFC-32日期格式爲短日期(MM/DD/YYYY)與ASP經典

我有這樣的格式:

Wed, 10 Jun 2009 12:46:13 +0000 

,我需要得到它成這種格式:

06/10/2009 

到目前爲止,我一直在擺弄這個RSS提要腳本ASP:

<% 
' change the RSSURL variable to the exact URL of the RSS Feed you want to pull 
RSSURL = "{url to rss feed}" 

Dim objHTTP ' this object is used to call the RSS Feed remotely 
Dim RSSURL,RSSFeed ' these variables hold the URL and Content for the RSS Feed 
Dim xmlRSSFeed ' this variable hold the XML data in a DOM Object 
Dim objItems,objItem, objChild ' these variables are used to temporarily hold data from the various RSS Items 
Dim title,description,link ' these are local variables that will hold the data to be displayed 
Dim pubDate 
Dim RSSOutput ' variable will hold the HTML that was converted from the RSS Feed 

' this code requests the raw RSS/XML and saves the response as a string <RSSFeed> 
Set objHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP") 
objHTTP.open "GET",RSSURL,false 
objHTTP.send 
RSSFeed = objHTTP.responseText 

' this code takes the raw RSSFeed and loads it into an XML Object 
Set xmlRSSFeed = Server.CreateObject("MSXML2.DomDocument.4.0") 
xmlRSSFeed.async = false 
xmlRSSFeed.LoadXml(RSSFeed) 

' this code disposes of the object we called the feed with 
Set objHTTP = Nothing 

' this is where you determine how to display the content from the RSS Feed 

' this code grabs all the "items" in the RSS Feed 
Set objItems = xmlRSSFeed.getElementsByTagName("item") 

' this code disposes of the XML object that contained the entire feed 
Set xmlRSSFeed = Nothing 

' loop over all the items in the RSS Feed 
For x = 0 to 3 
    ' this code places the content from the various RSS nodes into local variables 
    Set objItem = objItems.item(x) 
    For Each objChild in objItem.childNodes 
     Select Case LCase(objChild.nodeName) 
      Case "title" 
        title = objChild.text 
      Case "link" 
        link = objChild.text 
      Case "description" 
        description = objChild.text 
      Case "pubdate" 
        pubDate = objChild.text 
     End Select 
    Next 
    ' Format display output 
    RSSOutput = RSSOutput & "<tr><td valign='top' style='width:75px; height: 34px;' class='addresstext2'><b>"& pubDate &"</b></td><td valign='top'><a class=ccc href=""" & link & """>" & title & "</a></td></tr>"  

Next 

%>

當我從RSS獲得pubDate時,我認爲它是一個字符串,當我嘗試CDate時,出現類型不匹配,我也嘗試過Format()和相同的處理。任何人都可以提出一種方法來格式化這個日期到我需要的?

謝謝!

回答

2

Rss使用RFC822中指定的格式。

this thread發現了一個功能:

function parseRSSDate(sRSSDate) 
' take RFC822-formatted date string and return VBScript date object 
' ie: "Fri, 13 Jun 2008 16:33:50 GMT" 

dim sDay, sMonthName, sMonthNum, sYear, sHour, sMinute, sSecond 
dim oRE, oMatches, oMatch 
dim sDate, oDate 

set oRE = new regexp 
    oRE.IgnoreCase = True 
    oRE.Global  = True 
    oRE.Pattern  = "^([A-Za-z]{3}),\s([0-9]{1,2})\s([A-Za-z]{3})\s([0-9]{4})\s([0-9]{2}):([0-9]{2}):([0-9]{2})" 
    set oMatches = oRE.Execute(sRSSDate) 
     if oMatches.count > 0 then 
      set oMatch = oMatches(0) 
       sDay  = oMatch.SubMatches(1) 
       sMonthName = oMatch.SubMatches(2) 
       sMonthNum = monthVal(sMonthName) 
       sYear  = oMatch.SubMatches(3) 
       sHour  = oMatch.SubMatches(4) 
       sMinute  = oMatch.SubMatches(5) 
       sSecond  = oMatch.SubMatches(6) 
       sDate = sMonthNum & "/" & sDay & "/" & sYear 
       oDate = cDate(sDate) 
      set oMatch = nothing 
     end if 
    set oMatches = nothing 
set oRE = nothing 
parseRSSDate = oDate 
end function 

它也要求一個名爲monthVal功能,它只是返回一個數字一個月名稱:

代碼:

function monthVal(sMonthName) 
' return month number (1-12) from month name 
dim rv 
dim aMonths : aMonths = Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") 
for i = 0 to uBound(aMonths) 
    if sMonthName = aMonths(i) then rv = i+1 
next 
monthVal = rv 
end function 
+0

真棒,那很快!謝謝,我不能相信我錯過了那裏,我無法通過谷歌找到RSSDate格式的任何結果。 – Jakub 2009-08-18 12:57:02

+0

沒問題。我知道rss日期格式是RFC822,所以我通過與vbscript和/或asp解析進行了搜索。 – Espo 2009-08-18 12:58:40