ASP: Convert Date To RSS Date


Introduction

Author: Martin Warning
Year: 2007
License: Free

In RSS feeds the date is used in various fields like <PubDate> and <lastBuildDate>. The problem is that the date has to be formatted in a specific way to be a valid date. The format used in RSS is: Sun, 20 May 2012 18:13:28 GMT. One issue to take into account is that dates are in GMT time. This means that you will need to change the time in your date to it's GMT variant.

The first example doesn't take the GMT offset in account, but the second example does take GMT into account and allows the user to set the Daylight Saving Time (DST) time as well. This is needed as GMT doesn't have a DST so the GMT offset changes during DST.

Code

Languages: The following code is an example to convert date to RSS date:
<%
' Function to convert dates to RSS especification dates

Function fncRSSDate(strDate)
	Dim intHour : intHour = Hour(strDate)
	Dim intMin : intMin = Minute(strDate)
	Dim intSec : intSec = Second(strDate)

	If intHour < 10 Then
		intHour = "0" & intHour
	End If
	If intMin < 10 Then
		intMin = "0" & intMin
	End If
	If intSec < 10 Then
		intSec = "0" & intSec
	End If

	fncRSSDate = WeekdayName(Weekday(strDate), TRUE) & ", " & Day(strDate) & " " & MonthName(Month(strDate), TRUE) & " " & Year(strDate) & " " & intHour & ":" & intMin & ":" & intSec & " GMT"
End Function
%>

This code will only convert a date send to it to a RSS compatible date. You would call this function like fncRSSDate(Now()) to get the RSS variant of in this case the current time (at page load).


The following code allows to configure both the GMT and DST offset:
<%
' Function to convert dates to RSS especification dates

Function fncRSSDate(strDate)
	' Indicate how many hours your time zone differs from GMT
		' use a negative value if you are east of GMT
		' use .5 if you have a Offset that includes half a hour
	Dim intGMTOffset : intGMTOffset = 6		'hour(s)

	' Daylight saving settings, you only need to change the first part month/date and the time if so required
	Dim strDSTStart : strDSTStart = cDate("3/8/" & Year(Now()) & " 2:00:00 AM")
	Dim strDSTEnd : strDSTEnd = cDate("11/1/" & Year(Now()) & " 2:00:00 AM")
	' Indicate how many hours DST differs from normal time in your timezone
	Dim intDSTOffset : intDSTOffset = 1		'hour(s)

	If Now() > strDSTStart AND Now() < strDSTEnd Then
		strDate = strDate + ((intGMTOffset - intDSTOffset)*(1/24))
	Else
		strDate = strDate + (intGMTOffset*(1/24))
	End If

	Dim intHour : intHour = Hour(strDate)
	Dim intMin : intMin = Minute(strDate)
	Dim intSec : intSec = Second(strDate)

	If intHour < 10 Then
		intHour = "0" & intHour
	End If
	If intMin < 10 Then
		intMin = "0" & intMin
	End If
	If intSec < 10 Then
		intSec = "0" & intSec
	End If

	fncRSSDate = WeekdayName(Weekday(strDate), TRUE) & ", " & Day(strDate) & " " & MonthName(Month(strDate), TRUE) & " " & Year(strDate) & " " & intHour & ":" & intMin & ":" & intSec & " GMT"
End Function
%>

The intGMTOffset, strDSTStart, strDSTEnd and intDSTOffset variables will need to be configured to meet your GMT and DST needs. intGMTOffset is used to indicate the ammount of difference in time between your location and GMT. Be aware that half an hour is indicate with .5. strDSTStart and strDSTEnd are used to indicate the start and end of DST, the year in this case should not be changed so this functions works correctly in the following years (until DST dates are changed). intDSTOffset indicates how much the DST differs from the normal time in your location.


Further reading:
RSS (Wikipedia)


Printed from: http://flyinglowlander.com/ (5/20/2012)
© FlyingLowander.com 2006 - 2012

Visit http://flyinglowlander.com for more XHTML, CSS, ASP and JavaScript examples, templates and tutorials.