[英]convert iso8601 date format to date object
我收到的日期格式的摘要,我想在HTML表中显示日期,但是无法将iso8601类型的日期格式转换为日期对象。
$(document).ready(function(){
var startDate;
var formattedDate;
var day;
$(function(){
$.ajax({
url: "http://localhost:8080/employee/101",
method: "Get",
success: function(data,status){
startDate = new Date(data.joinDate);
day = startDate.dayOfWeek;
console.log(startDate);
}
});
});
我收到错误消息:“ console.log(startDate);”处的日期无效
我还需要增加日期,但是在将iso8601格式转换为普通日期对象时遇到了麻烦。
既然您拥有了一切,那么您的employee/101
数据响应就不会出现:
var data = { dayOfMonth: 14, monthValue: 11, // NOVEMBER, we need to -1 it since Date expects 0 based month value year: 2017, } var dateObject = new Date(data.year, data.monthValue - 1, data.dayOfMonth); alert(dateObject)
在我的语言环境中:
2017年11月14日星期二00:00:00 GMT + 0100(中欧标准时间)
和BTW startDate.dayOfWeek
给出startDate
为Date对象,则不能使用dayOfWeek
像你这样。
感谢您的所有帮助。 我终于按如下方式工作:我可以显示日期,尽管我需要做一些修改才能将日期和月份显示为ÝYY。但是我仍然认为这是一个很好的进步。
javascript:
<script type="text/javascript">
$(document).ready(function(){
var dateObj;
$(function(){
$.ajax({
url: "http://localhost:8080/employee/101",
method: "Get",
success: function(data,status){
dateObj = new Date(data.joinDate.year,
data.joinDate.monthValue - 1,
data.joinDate.dayOfMonth);
console.log(dateObj);
$('#timeTable th:not(:first-child)').each(function(){
if($(this).attr('Id') === 'totalHours') return;
$(this).text(dateObj.getDate()+'-'+dateObj.getMonth()+'-'+dateObj.getFullYear());
dateObj.setDate(dateObj.getDate() + 1);
});
}
});
});
});
</script>
我的html表:
<table Id="timeTable" class="table table-bordered">
<thead>
<tr>
<th>Projects</th>
<th id="mon" style="width:100px"></th>
<th id="tue" style="width:100px"></th>
<th id="wed" style="width:100px"></th>
<th id="thu" style="width:100px"></th>
<th id="fri" style="width:100px"></th>
<th id="sat" style="width:100px"></th>
<th id="sun" style="width:100px"></th>
<th id="totalHours" style="width:100px"></th>
</tr>
</thead>
<tbody>
<tr>
<td>Task1</td>
<td contenteditable='true'></td>
<td contenteditable='true'>2</td>
<td contenteditable='true'></td>
<td contenteditable='true'>3</td>
<td contenteditable='true'>4</td>
<td contenteditable='true'> </td>
<td contenteditable='true'> </td>
<td contenteditable='true'> </td>
</tr>
<tr>
<td contenteditable='true'>Task2</td>
<td contenteditable='true' >5</td>
<td contenteditable='true' >2</td>
<td contenteditable='true' >2.5</td>
<td contenteditable='true' >3</td>
<td contenteditable='true' >4</td>
<td contenteditable='true' > </td>
<td contenteditable='true' > </td>
<td contenteditable='true' > </td>
</tr>
<tr>
<td>Task3</td>
<td>5</td>
<td>2</td>
<td>2.5</td>
<td>3</td>
<td>4</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.