Search This Blog

Wednesday, November 4, 2009

Comparing Dates in Sharepoint XSLT

I’ve recently come across development requires date comparison before populating the items.

This cannot be done directly through XPATH query through comparative operator accept “=” and “!=”, syntax like “>”, “<” and etc doesn’t work as expected.

Thus the workaround is to convert the DateTime string to number and removing any symbol usually found in DateTime format. Exp: “-”, “/”.

Example

Normal DateTime format
= 2009-07-21T00:34:16Z
 
After conversion

code: number(translate(substring(@StartDate,1,10),'-',''))

= 20090721
 
Compare DateTime
<xsl:if test="number(translate(substring(@StartDate,1,10),'-','')) &gt; number(translate(substring(@EndDate,1,10),'-','')) ">
….
</xsl:if>

There you go, a simple way to compare DateTime in XSLT.

Credit goes to Kode's thoughts

Format Sharepoint DateTime

A quick tips to formatting DateTime in XSLT for Content Qury Webpart through Editing Sharepoint ItemStyle or manually configure Dataview Webpart.

To achieve the desire format, there are string variable needed. (LCID & FormatFlag

LCID code is the localize language used by the hosting machine. Full listing of LCID code could be found Here. For illustration purpose, English-United State (1033) is used in following example. 

FormatFlag is the format apply to the date and time itself. FormatFlag can be apply using number or string format. A sample of numbering FormatFlag of en-US is available in the image below(Source). Full listing of FormatFlag for each language Here.

y1pvXScwycRj8VxEvHnvzscNrP8dWbGir_SV9on3kytIB4-3DSO1yoEMh4bJzAvWbjKNiC7pTF0By4[1] 

As for the text format a full list of FormatFlag can be found Here.

Example:

Numbering FormatFlag
ddwrt:FormatDate(string(@EventDate), 1033, 5)
= 5/9/2007 12:00 AM
 
String FormatFlag
ddwrt:FormatDate(string(@EventDate), 1033, d/m/yyyy)
=5/9/2007
 
ddwrt:FormatDate(string(@EventDate), 1033, dd/m/yyyy)
=05/9/2007
 
ddwrt:FormatDate(string(@EventDate), 1033, dd/mmm/yyyy)
=05/Sep/2007

Other useful resources:

Monday, November 2, 2009

[Tutorial] How to customize “Tiny” SharePoint calendar

 

Before

After

4-11-2009 2-02-00 PM tinycalendar

Hey, i have amended the tutorial so it looks better and shows respect for people’s liberty of the press. Hehe..

reference: A tiny cute SharePoint calendar (Part I) & A tiny cute SharePoint calendar (Part II) by pathtosharepoint.wordpress.com