1. Change your existing template to:
<xsl:template match="messagedate">
<xsl:call-template name="check-date">
<xsl:with-param name="date" select="./messagedate"/>
</xsl:call-template>
</xsl:template>
2. Write the named template:
<xsl:template name="check-date">
<xsl:param name="date"/>
<xsl:variable name="cc" select="substring($date,1,2)"/>
etc: as your existing code, but changing $messageDate to $date.
3. "I have to make this for all elements like messagedate" - call the named template for other date elements in the same way as it is called above for messagedate.
4. "it should also give the attribute name as the element name in which I have to use it." - I don't understand this part of your question.
Really, you should be checking leap years. That means you are better off using a standard library routine rather than coding this yourself. If you can, move to XSLT 2.0 and use "castable as xs:date".
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference