如何将列表从LocalDate转换为百万美元的Javascript日期列表?
当我在
中添加列表时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 :(得分:3)
我知道这是一个古老的问题,但仍然有效。因此,这是我认为可以满足OP要求的解决方案。使用Thymeleaf 3:
const nonWorkingDays = [];
[# th:each="date : ${#temporals.listFormat(nonWorkingDaysFromDb, 'yyyy-MM-dd')}"]
nonWorkingDays.push(new Date([[${date}]]));
[/]
其结果为:
const nonWorkingDays = [];
nonWorkingDays.push(new Date("2019-01-01"));
nonWorkingDays.push(new Date("2019-02-01"));
注意:
如果您要使用本地时区创建日期,则可以执行以下操作:
const nonWorkingDays = [];
[# th:each="date : ${nonWorkingDays}"]
nonWorkingDays.push(new Date([[${date.year}]], [[${date.monthValue - 1}]], [[${date.dayOfMonth}]], 0, 0, 0));
[/]
答案 1 :(得分:1)
目前,在找到更好的答案之前,我在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]);
});