volume_up

A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More

volume_up

A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More

Problem with substring from markup

Hi!

I'm using a repeater and the following in markup:

<%# ((EPiServer.Core.PageData)Container.DataItem)["Date"]%>

I really would like to shorten the date with a substring method. I just don't get it to work... How can I do this?

#73636
Aug 02, 2013 12:42
Vote:

What property type is your "Date"? If it is a DateTime, you cannot make a substring on it since substring is a method on a string.

To get a property value as a DateTime you need to cast it, for example: (DateTime)page["Date"]

See http://world.episerver.com/Articles/Items/Best-Coding-Practices/ for some best practices about casting Properties and preparing for null values.

If you want to convert a DateTime to a string you need to perform a .toString() on it. Since there are ways to format a string on the DateTime.toString() method I think it would be better to use it instead of performing .toString().SubString();

See http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx and http://msdn.microsoft.com/en-us/library/az4se3k1.aspx for more information about DateTime string formats

#73637
Aug 02, 2013 14:29

I got it to work with the casting... Thanks!

#73727
Aug 07, 2013 10:01
error This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.