p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   get the current system data and time (http://p2p.wrox.com/showthread.php?t=67940)

arunprasadlv May 6th, 2008 01:16 PM

get the current system data and time
 
Hi,

Is there any function to get the current system date and time in long format (20080303 hh:mm:ss:mi EST).

Thanks,
Arun


iceandrews May 6th, 2008 01:48 PM

It is my understanding that you can make use of the XPath function 'current-dateTime()' to retrieve the system time.

This would provide you with the current system time in the xs:dateTime type.

You could then make use of the format-dateTime() function to restructure the time in the format that you require. You can basically customize the way you want to output the current time.

A very simple style sheet with no input document.

Code:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />

<xsl:template match="/">
<html>
<body>
<xsl:value-of select="format-dateTime(current-dateTime(), '[Y,4][D,2][M,2] [H]:[m]:[s] [Z]')" />
</body>
</html>       
</xsl:template>

</xsl:stylesheet>

This stylesheet will output the following:
Code:

<html>
  <body>20080605 14:45:04 -04:00</body>
</html>

Good luck!

arunprasadlv May 6th, 2008 04:30 PM

Hi,

The below output looks good but it is missing the 'milliseconds'in the time format .Is there any function to get the time in long format including milli seconds

"20080605 14:45:04:59 -04:00"

Thanks,
Arun


mhkay May 7th, 2008 05:24 AM

There's a function current-dateTime() in XPath 2.0, and a function format-dateTime() in XSLT 2.0 that will format it the way you want - except for the EST bit (which might not work reliably because the function doesn't necessarily know that you're in the United States in winter; all it knows is the timezone offset -05:00)

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

mhkay May 7th, 2008 05:27 AM

The precision of the date/time is implementation-defined, though all implementations are supposed to provide at least 3 digits (millisecond precision).

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

joefawcett May 7th, 2008 05:47 AM

Quote:

quote:Originally posted by mhkay
 There's a function current-dateTime() in XPath 2.0, and a function format-dateTime() in XSLT 2.0 that will format it the way you want - except for the EST bit (which might not work reliably because the function doesn't necessarily know that you're in the United States in winter; all it knows is the timezone offset -05:00)

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Winter in the US? I thought he was asking for Eastern Summer Time in Australia :)

--

Joe (Microsoft MVP - XML)

iceandrews May 7th, 2008 07:43 AM

Quote:

quote:Originally posted by arunprasadlv
 Hi,

The below output looks good but it is missing the 'milliseconds'in the time format .Is there any function to get the time in long format including milli seconds

"20080605 14:45:04:59 -04:00"

Thanks,
Arun


With milliseconds

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" />

<xsl:template match="/">
<html>
<body>
<xsl:value-of select="format-dateTime(current-dateTime(), '[Y,4][D,2][M,2] [H]:[m]:[s]:[f01] [Z]')" />
</body>
</html>
</xsl:template>

</xsl:stylesheet>



All times are GMT -4. The time now is 09:44 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.