Best practice for Joda DateTime formatting in views (Scala Play 2.1)

2,612 views
Skip to first unread message

Kirill Grishin

unread,
Dec 11, 2012, 3:03:57 AM12/11/12
to play-fr...@googlegroups.com
Hi,

I could not find anything in the group or google so I decided to ask here.

I am using Play 2.1 with Scala.

How do I display a lot of formatted Joda DateTime dates in a view?

Now I am doing this in my views:

@import org.joda.time.format.DateTimeFormat

@defining(DateTimeFormat.forPattern("yyyy-MM-dd")) { dateFormatter =>
  ....
  <ul>
    @for(product <- products) {
      <li>@product.name ( @dateFormatter.print(product.registeredDate) )</li>
     }
  </ul> 
  ...
}

Is this a correct way to do this? Is there a better way to do the same thing?

Thank you,
Kirill


Guillaume Bort

unread,
Dec 11, 2012, 3:50:38 AM12/11/12
to play-fr...@googlegroups.com
You can declare a kind of template `tag`. Just provide somewhere another template, a def, or a Java static method that do that for you.

@format(product.registeredDate)

Written in Scala, it would be something like:

def format(date: DateTime, pattern: String = "yyyy-MM-dd") = DateTimeFormat.forPattern(pattern).print(date)





--
 
 



--
Guillaume Bort, http://guillaume.bort.fr

m e

unread,
Dec 11, 2012, 3:02:38 PM12/11/12
to play-fr...@googlegroups.com
Hi 

this is ace been looking for something like this for weeks :)

Danny
 

--
 
 

Reply all
Reply to author
Forward
0 new messages