简体   繁体   中英

play framework : Pass date to view

I need to pass the date parameter to view in play framework

My controller looks like

something.render(new Date());

And on my view what I've done is

@(myDate : Date)
<script lang="text/javascript">
   var time = "@(myDate)";
</script>>

This time variable I further need to use in jQuery. Thing is Play framework is converting the date to string object. What I want is date object itself.

If I remove the quotes around "@(myDate)" Java script gives following output.

var backupTimeString = 2015-01-15 00:01:28.767;
Uncaught Syntax Error : expecte number

I really need the object to passed as Date object not as String represnetation of Date

1) If you work in local time, you could pass the time as a formatted string :

something.render( ... new java.text.SimpleDateFormat("yyyy/MM/dd hh:mm:ss").format(new java.util.Date()) ...)

and convert it to javascript date in the view :

<script>
  var t = new Date("@mydate");
</script>

According to http://dygraphs.com/date-formats.html the format aaaa/mm/jj hh:mm:ss is the most robust.

2) In case you don't work in local time, recent browsers accept ISO-8601 date with offset from UTC, for example :

new Date('2015-01-22T12:00-0600')

3) As a last resort, you can pass a timestamp :

something.render(... new java.util.Date().getTime() ...)

<script>
var t = new Date(@mydate);
</script>

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