In a xslt macro I'm listing an item properties and values. With dates I'm getting this format like:
2007-07-01T00:00:00
Why is this happening and how can I avoid it. I would like to not have to hardcode date fields in the macro. If a user inserts a date that looks like 2007-07-01 why would it be presented like 2007-07-01T00:00:00?
That format is a standard date/time data type storage format. It can be changed (reformatted) very easily in XSLT to give you full control over how the date/time is formatted. There's an old wiki page on this which gives examples: http://en.wikibooks.org/wiki/Umbraco/Reference/umbraco.library/FormatDateTime.
How can we make the code in a more general fashion, so it works with any date field? Do we have to hardcode each field name that is a date? So every time we have a new date field we have to change our macro? This is what I'd like to achieve.
What I have done in the past is actually write my own date format function that uses a setting stored in the web.config file, that way if the client wishes to change the date format used across their entire site, they can just change the mask in the web.config
I've been doing this to keep consistent dates/times on pages: I have a template that matches any element or attribute in the "date" mode, which I can then use to format all the date values, whether they're standard Umbraco attributes (like createDate + updateDate) or DatePicker values:
<xsl:template match="/">
<p>
I was born on:
<xsl:apply-templates select="$currentPage/dateOfBirth" mode="date" />
</p>
<p>
Page updated on:
<xsl:apply-templates select="$currentPage/@updateDate" mode="date" />
</p>
</xsl:template>
<!-- Template for any value that should be output as a date -->
<xsl:template match="* | @*" mode="date">
<time datetime="{.}">
<xsl:value-of select="umbraco.library:FormatDateTime(., 'd MMM yyyy')" />
</time>
</xsl:template>
(Usually I just use XSLT to format the date, but I guess most people know the strftime() equivalents for use with the library function...)
hmm, I can not apply this in a general way, I have to apply it on specified fields. I'm looking for a way to have a generic macro, that formats dates when they appear...
so this template should only be applied to date fields, automatically. I'm trying out to test castable but I get errors from umbraco...
The problem starts with how umbraco saves a date entered by the user, eg "01-01-2000", and not returning it the same way ("01-01-2000T000000").
dates like 2007-07-01T00:00:00
Hey there.
In a xslt macro I'm listing an item properties and values. With dates I'm getting this format like:
2007-07-01T00:00:00
Why is this happening and how can I avoid it. I would like to not have to hardcode date fields in the macro.
If a user inserts a date that looks like 2007-07-01 why would it be presented like 2007-07-01T00:00:00?
Thanks,
Duarte
Hi Duarte,
That format is a standard date/time data type storage format. It can be changed (reformatted) very easily in XSLT to give you full control over how the date/time is formatted. There's an old wiki page on this which gives examples: http://en.wikibooks.org/wiki/Umbraco/Reference/umbraco.library/FormatDateTime.
Hope this helps...
Hi Duarte,
It is also work having a look at the new Wiki and the EXSLT date functions.
Cheers,
Chris
Thanks for your assistance.
How can we make the code in a more general fashion, so it works with any date field? Do we have to hardcode each field name that is a date? So every time we have a new date field we have to change our macro? This is what I'd like to achieve.
Duarte
Hi Duarte,
What I have done in the past is actually write my own date format function that uses a setting stored in the web.config file, that way if the client wishes to change the date format used across their entire site, they can just change the mask in the web.config
Cheers,
Chris
Hi Duarte,
I've been doing this to keep consistent dates/times on pages: I have a template that matches any element or attribute in the "date" mode, which I can then use to format all the date values, whether they're standard Umbraco attributes (like createDate + updateDate) or DatePicker values:
/Chriztian
Chriztian,
That's brilliant, and soo simple!! I'll give it a try tomorrow.
Thanks.
Duarte
hmm, I can not apply this in a general way, I have to apply it on specified fields. I'm looking for a way to have a generic macro, that formats dates when they appear...
so this template should only be applied to date fields, automatically. I'm trying out to test castable but I get errors from umbraco...
The problem starts with how umbraco saves a date entered by the user, eg "01-01-2000", and not returning it the same way ("01-01-2000T000000").
Duarte
ok, used a modified version of Chriztian's template approach:
Duarte
is working on a reply...