简体   繁体   中英

grails change Date format in gsp view

When I try to use date format tag in gsp view to change the format of my date but it doesn't work.

This is my code:

class MyDate {  
    Date date
}

MyDateController:

....
def unixSeconds = params["date"].replaceAll("\"", "") as long  //params["date"]="1386157660"
Date date = new Date(unixSeconds*1000L)
myDateInstance = new MyDate(date:date)
....

gsp View:

${myDateInstance.date.format('yyyy-MM-dd HH:mm')}

The format that I have is 2013-12-04 12:47:40.0 instead of 2013-12-04 12:47

Afaict, that shouldn't happen and I can't see how it is happening...

Are you sure that's the line of code that's generating the output you're seeing?

You could try the Grails formatDate tag in its place:

<g:formatDate format="yyyy-MM-dd HH:mm" date="${myDateInstance.date}"/>

您可以在value属性中使用“formatDate”,如下所示

<g:textField name="saleDate" value="${formatDate(format:'dd-MM-yyyy',date:saleItem?.saleDate)}"/>

In addition to @tim_yates's answer above, there is another point I want to add:

I observed that if type and style are given, format is not used. I think they work as alternatives for each other. Also, using format would be beneficial as the Date string format could differ on different operating systems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM