Link to home
Start Free TrialLog in
Avatar of intikhabk
intikhabk

asked on

Display datetime in XSLT

how get a current date time value in XSLT. the date value should be in the format MM/DD/YYYY
Avatar of Gertone (Geert Bormans)
@chaituu,

I am very pleased that you reference my answers to a similar question, but...
- you could have removed the QueryTermInfo from the links you posted. Now it is obvious that as an answer to this question you simply queried the EE database. I can tell you that just googling for an answer without critically looking at the results is NOT an EE experts correct attitude.
- If you would have understood the links you are referring to, you would have realised that this is only a partial answer to the question asked, working in a very limited scope. The first link gives the current-date, only if EXSLT is supported by the processor. Not all processors do support EXSLT date functions to the same extend. You could have warned for that. The second link you post is a very limited approach from one date format to another and is not likely to work in this case

@intikhabk,

In order to answer this question, it is important to know which processor you are using
- If you are using XSLT2, there is a current-date() function that gives you all you need, you can reformat with a simple regular expression then
- If you are using msxml, saxon, xalan or some other, you have access to exslt (verify in the date functions section of www.exslt.org). You can then use a solution similar to what you find in chaituus first reference. This solution uses exernal functions, so they are pretty bound to a specific processor. Make sure you get the namespace right
- If your processor is XSLT1 and is not EXSLT aware, you will not be able to access the current date from whitin the Stylesheet and you will have to pass the current date from your application as a parameter to the XSLT. It would be interesting to know how your architecture looks like
As soon as you figured out what the best approach be for you, I will be happy to help you with the code

cheers

Geert
I thought the links posted above may help him in solving the problem.what is QueryTermInfo you are reffering to?
just look at the URL
https://www.experts-exchange.com/questions/22837627/How-to-get-the-current-date-in-XSLT.html?sfQueryTermInfo=1+current+date+xslt
is what you posted
https://www.experts-exchange.com/questions/22837627/How-to-get-the-current-date-in-XSLT.html
is the link to the question without showing that you just used the EE search engine
Posting links without annotation is dangerous and should not be done.
Only post links if you know what the link ends limitations are.
People asking questions on EE can use the search engine to, always assume that they have done that and were not helped.
from next time onwards i will keep in mind while posting these links.
If your using .Net without EXSLT you can create an Extension class that can provide functions to your xslt. Functions such as CurrentDate and FormatDate can be easily created in .Net

If so I can sort out an example for you.
mmh, for .Net, I tend to use the JScript solution, based on the EXSLT msxml scripts.
These have the advantage that they work for all usage of msxml, both in .Net or outside this.
I added the external function (and a usage example) that gives you what you need for msxml in and outside .Net
It will not work for other processors, that is why it is important that we know which processor you are using.
If you need your XSLT to be processor independent, only passing the parameter is an option
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:date="http://exslt.org/dates-and-times"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                extension-element-prefixes="date msxsl">
 
<msxsl:script language="JScript" implements-prefix="date">
	<![CDATA[
	function date(){
		var oDate = new Date();
		var ret = "";
		var m = oDate.getMonth() + 1;
	    var mm = m < 10 ? "0" + m : m;
		ret = ret + mm + "/";
		var d = oDate.getDate();
		var dd = d < 10 ? "0" + d : d;
		ret = ret + dd + "/";
		ret = ret + oDate.getFullYear();
		return ret;
		}
	]]>
</msxsl:script>
<xsl:template match="/">
	<xsl:value-of select="date:date()"/>
</xsl:template>
 
</xsl:stylesheet>

Open in new window

Avatar of intikhabk
intikhabk

ASKER

I will try and check if it works...thanks for the answer.......
ASKER CERTIFIED SOLUTION
Avatar of Tony McCreath
Tony McCreath
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial