2017-02-10 37 views
0

如何將列表從LocalDate轉換爲百里香JavaScript日期列表?將列表從LocalDate轉換爲Javascript日期在thymeleaf

當我剛剛在

var timeaxis = /*[[${timeaxis}]]*/ []; 

添加列表,然後我得到的LOCALDATE的改裝成JSON

var timeaxis = [{'chronology':{'calendarType':'iso8601','id':'ISO'},'dayOfMonth':1,'dayOfWeek':{'$type':'DayOfWeek','$name':'TUESDAY'},'dayOfYear':306,'era':{'$type':'IsoEra','$name':'CE'},'leapYear':true,'month':{'$type':'Month','$name':'NOVEMBER'},'monthValue':11,'year':2016}]; 

當我試圖用格式化的日期https://github.com/thymeleaf/thymeleaf-extras-java8time

var timeaxis = /*[[${#temporals.listFormat(timeaxis, "'new Date('yyyy','MM','d')'")}]]*/ []; 

然後我得到一串字符串

var timeaxis = ['new Date(2016,11,1)']; 

我想要的是[new Date(2016,11,1)]如何在Thymeleaf中實現此目標?

+0

我不能重現這個...在thymeleaf 3,我越來越:'VAR timeaxis = [新日期(2017,02,10),新日期(2017,02,10),新日期(2017,02,10)];'。你在使用百里香2嗎? – Metroids

+0

@Metroids謝謝你的回答。我用的是thymeleaf 2.我也用thymleaf 3.0.1試過它,但它仍然不起作用。與thymeleaf 3我得到:'[{「年」:2016年,「月」:「11月」,「dayOfMonth」:1,「dayOfWeek」:「星期二」,「時代」:「CE」,「dayOfYear」:306 「leapYear」:真 「monthValue」:11 「年表」:{ 「ID」: 「ISO」, 「calendarType」: 「ISO8601」}}]' – leo

回答

0

目前,等到一個更好的答案被發現,我解決了問題,在JavaScript

var timeaxis = /*[[${#temporals.listFormat(timeaxis, 'yyyy-MM-dd')}]]*/ []; 
var dates = $.map(timeaxis, function (date) { 
    var parts = date.split('-'); 
    //please put attention to the month (parts[0]), Javascript counts months from 0: 
    // January - 0, February - 1, etc 
    return new Date(parts[0], parts[1] - 1, parts[2]); 
});