Format Default Date from datasource in DMXzone Calendar using ASP Support
Question:
How can I get a date from a datasource and format it the correct way to be used as Default Date in DMXzone Calendar?
Answer:
Note: All credits go to Miro for writing this code! Comments are left in the code for explanation!
When trying to get a Default Date from a datasource, it seems that the only Date Format that the DMX Calendar can handle is m,d,y. The following code includes a function for preformating the date that comes from any source into a suitable format for the DMX Calendar. Once the Calendar gets an understandable date format it will convert it in any format you have set into the Date Format field in Property Inspector.
Create a page that contains a Calendar field. Set the Date Format the way you want to. Copy the code below and paste at the top of your document containing the calendar.
<%
'Here we'll convert date from database into a format that Calendar understands e.g. mm-dd-yy
'I guess there will be no problem to handle a string date as well since we use CDate function
'so in place of clndrDefDate value we can set any variable - Session, URL Param etc.
Dim clndrDefDate
If isDate(Recordset1.Fields.Item("date").Value) then
clndrDefDate = CDate(Recordset1.Fields.Item("date").Value)
varFld = CDate(clndrDefDate)
intMonth = Month(varFld)
intDay = Day(varFld)
intYr = Year(varFld)
If intMonth < 10 Then
strMonth = "0" & CStr(intMonth)
Else
strMonth = CStr(intMonth)
End If
If intDay < 10 Then
strDay = "0" & CStr(intDay)
Else
strDay = CStr(intDay)
End If
strYr = Right(CStr(intYr), 4) ' And change the 4 to 2 for 2 year dates.
clndrDefdate = CStr( strMonth & "/" & strDay & "/" & strYr)
Else
Response.Write("Bad date formatting!")'error handling functions here
End If
%>
Edit the name for the Recordset and the name for the Date field to correspond with the recordset and date field in use.
Next, edit the Calendar code. Find the line that starts with defaultDate and edit it so that it is like the code below. Simply add new Date('<%=(clndrDefdate)%> to the defaultDate.
defaultDate : new Date('<%=(clndrDefdate)%>'),//here we pass our newly formated string and calendar will transform it into any format we like since it already understands the date that we are passing
Save your document and upload it to your server.
That's basically it. Preview the result on a live page and see what happens!
I'd really appreciate your feedback on your results! If you have any more questions just let me know or post your questions in the DMX Calendar Forum!
Comments
Be the first to write a comment
You must me logged in to write a comment.