编辑: 我创建了一个我想要做的例子:http://plnkr.co/edit/HvItQP2uDooaMgK7GRQe?p=preview
它有几个问题。
它在FF(在MAC上)不起作用。没有我能看到的错误。
在选择11-04-17至17-04-17之后的Chrome中它仍然显示30/03/2017行
我非常感谢大家的帮助,请记住,我上周刚开始使用JS。我可以阅读代码并且(可能)理解逻辑,但我没有达到可以清楚地看到如何实现它的水平。
我正在使用数据表按日期对一些基本数据进行排序。我复制了一个很好的例子here:。示例日期采用mm / dd / yyyy格式。
我需要它以dd / mm / yyyy运行。当我将示例中的日期切换为我需要的格式时,它不再有效。
我是JS的新手,所以我想这就是魔术的一部分:
list.count
minDateFilter抓取输入中的数据,而.getTime()将其转换为1970年的秒数。如果我可以开始使用它,那就开始搜索 .getTime(dateFormat:" dd-mm-yyyy")但还没有运气。
如果有人能帮助我,我会很感激。我总是可以使用隐藏的字段而不是。
谢谢
答案 0 :(得分:2)
getTime
以秒为单位返回unix时间戳,而不是17/04/2017
之类的日期。您必须使用toLocalDateString
。
var date = new Date();
console.log(date.toLocaleDateString("en-GB"));

答案 1 :(得分:1)
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/blur">
http://jsbin.com/sosurohaje/edit?console
说明:
d.getDate返回该月的日期,
d.getMonth返回0-11的月份(这就是+1的原因)
.getYear从1900年开始返回年份,这就是我使用modulo 100格式化它的原因:
d = new Date(date);
f=d=>(d+"").length==2?""+d:"0"+d;
minDateFilter= f(d.getDate())+"-"+f(d.getMonth()+1)+"-"+f(d.getYear()%100);
感谢oen44 ive包含了一个函数 f ,如果它不是一个两位数字,它将添加一个前导0 ...
答案 2 :(得分:1)
我会String#split字符串,然后将其解析为Date。 根据UTC,您将以毫秒为单位获得不同的值(除非您恰好是GMT时区)。如果您只是将它用于排序参考,那么使用哪个值无关紧要,只要您对所有值使用相同的类型。
function getTime(value, utc) {
// with dd/mm/yyyy then part[0] will be dd,
// part[1] will be mm and part[2] will be yyyy
var parts = value.split('/');
// Date requires a zero referenced month
parts[1] -= 1;
if (utc) {
return Date.UTC(parts[2], parts[1], parts[0]);
}
return new Date(parts[2], parts[1], parts[0]).getTime();
}
var value = '03/03/2016';
console.log(getTime(value));
console.log(getTime(value, true));
更新:这超出了您的实际问题,因此不再是主题。但是,答案与您的代码合并,没有保修。如果你不想自己这样做,还有可用的插件,包括moments
。
var getTime = function(value, utc) {
// with dd/mm/yyyy then part[0] will be dd,
// part[1] will be mm and part[2] will be yyyy
var parts = value.split('/');
// Date requires a zero referenced month
parts[1] -= 1;
if (utc) {
return Date.UTC(parts[2], parts[1], parts[0]);
}
return new Date(parts[2], parts[1], parts[0]).getTime();
};
var types = $.fn.dataTable.ext.type;
// Add type detection
types.detect.unshift(function(d) {
var ms = getTime(d);
var isValid = typeof ms === 'number' && !isNaN(ms);
return isValid ? 'date-x' : null;
});
// Add sorting method - use an integer for the sorting
types.order['date-x-pre'] = function(d) {
return getTime(d);
};
// Date range filter
var minDateFilter = '';
var maxDateFilter = '';
$.fn.dataTableExt.afnFiltering.push(
function(oSettings, aData, iDataIndex) {
if (typeof aData._date === 'undefined') {
aData._date = getTime(aData[0]);
}
if (minDateFilter && !isNaN(minDateFilter)) {
if (aData._date < minDateFilter) {
return false;
}
}
if (maxDateFilter && !isNaN(maxDateFilter)) {
if (aData._date > maxDateFilter) {
return false;
}
}
return true;
}
);
var oTable = $('#datatable').DataTable({
oLanguage: {
sSearch: 'Filter Data'
},
iDisplayLength: -1,
sPaginationType: 'full_numbers'
});
$('#datepicker_from').datepicker({
showOn: 'button',
dateFormat: 'dd/mm/yy',
buttonImageOnly: false,
onSelect: function(date) {
minDateFilter = getTime(date);
oTable.draw();
}
}).keyup(function() {
minDateFilter = getTime(this.value);
oTable.draw();
});
$('#datepicker_to').datepicker({
showOn: 'button',
dateFormat: 'dd/mm/yy',
buttonImageOnly: false,
onSelect: function(date) {
maxDateFilter = getTime(date);
oTable.draw();
}
}).keyup(function() {
maxDateFilter = getTime(this.value);
oTable.draw();
});
<link href="http://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.15/css/jquery.dataTables_themeroller.css" rel="stylesheet" />
<link href="http://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.15/css/jquery.dataTables.css" rel="stylesheet" />
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.2/css/smoothness/jquery-ui-1.10.2.custom.min.css" />
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.2/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.15/js/jquery.dataTables.min.js"></script>
<p id="date_filter">
<span id="date-label-from" class="date-label">From: </span><input class="date_range_filter date" type="text" id="datepicker_from" />
<span id="date-label-to" class="date-label">To:</span><input class="date_range_filter date" type="text" id="datepicker_to" />
</p>
<table width="100%" class="display" id="datatable">
<thead>
<tr>
<th>Date</th>
<th>ID</th>
</tr>
</thead>
<tbody>
<tr>
<td>30/03/2017</td>
<td><a href="42296/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000617 </a></td>
</tr>
<tr>
<td>30/03/2017</td>
<td><a href="42297/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000618 </a></td>
</tr>
<tr>
<td>30/03/2017</td>
<td><a href="42298/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000619 </a></td>
</tr>
<tr>
<td>03/04/2017</td>
<td><a href="42340/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000620 </a></td>
</tr>
<tr>
<td>03/04/2017</td>
<td><a href="42343/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000621 </a></td>
</tr>
<tr>
<td>03/04/2017</td>
<td><a href="42344/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000622 </a></td>
</tr>
<tr>
<td>03/04/2017</td>
<td><a href="42345/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000623 </a></td>
</tr>
<tr>
<td>03/04/2017</td>
<td><a href="42348/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000624 </a></td>
</tr>
<tr>
<td>03/04/2017</td>
<td><a href="42350/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000625 </a></td>
</tr>
<tr>
<td>04/04/2017</td>
<td><a href="42395/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000626 </a></td>
</tr>
<tr>
<td>05/04/2017</td>
<td><a href="42427/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000627 </a></td>
</tr>
<tr>
<td>05/04/2017</td>
<td><a href="42446/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000628 </a></td>
<tr>
<td>05/04/2017</td>
<td><a href="42458/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000629 </a></td>
</tr>
<tr>
<td>05/04/2017</td>
<td><a href="42461/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000630 </a></td>
</tr>
<tr>
<td>06/04/2017</td>
<td><a href="42490/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000631 </a></td>
</tr>
<tr>
<td>06/04/2017</td>
<td><a href="42491/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000632 </a></td>
</tr>
<tr>
<td>06/04/2017</td>
<td><a href="42493/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000633 </a></td>
</tr>
<tr>
<td>06/04/2017</td>
<td><a href="42494/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000634 </a></td>
</tr>
<tr>
<td>06/04/2017</td>
<td><a href="42497/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000635 </a></td>
</tr>
<tr>
<td>07/04/2017</td>
<td><a href="42531/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000636 </a></td>
</tr>
<tr>
<td>07/04/2017</td>
<td><a href="42532/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000637 </a></td>
</tr>
<tr>
<td>07/04/2017</td>
<td><a href="42533/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000638 </a></td>
</tr>
<tr>
<td>07/04/2017</td>
<td><a href="42539/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000640 </a></td>
</tr>
<tr>
<td>07/04/2017</td>
<td><a href="42540/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000641 </a></td>
</tr>
<tr>
<td>07/04/2017</td>
<td><a href="42541/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000642 </a></td>
</tr>
<tr>
<td>07/04/2017</td>
<td><a href="42542/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000643 </a></td>
</tr>
<tr>
<td>07/04/2017</td>
<td><a href="42544/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000644 </a></td>
</tr>
<tr>
<td>08/04/2017</td>
<td><a href="42565/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000645 </a></td>
</tr>
<tr>
<td>08/04/2017</td>
<td><a href="42566/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000646 </a></td>
</tr>
<tr>
<td>10/04/2017</td>
<td><a href="42604/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000647 </a></td>
</tr>
<tr>
<td>10/04/2017</td>
<td><a href="42607/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000648 </a></td>
</tr>
<tr>
<td>11/04/2017</td>
<td><a href="42636/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000649 </a></td>
</tr>
<tr>
<td>11/04/2017</td>
<td><a href="42638/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000650 </a></td>
</tr>
<tr>
<td>11/04/2017</td>
<td><a href="42639/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000651 </a></td>
</tr>
<tr>
<td>12/04/2017</td>
<td><a href="42661/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000652 </a></td>
</tr>
<tr>
<td>12/04/2017</td>
<td><a href="42664/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000653 </a></td>
</tr>
<tr>
<td>15/04/2017</td>
<td><a href="42711/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000654 </a></td>
</tr>
<tr>
<td>15/04/2017</td>
<td><a href="42712/service_request/CS_Reviewed" class="btn btn-default btn-sm">000000655 </a></td>
</tr>
</tbody>
</table>
答案 3 :(得分:0)
如果您有日期对象并且您知道输出格式,则可以使用日期方法构造输出字符串,例如:
var date = new Date("05/26/1984");
var formatDate = curdate.getDate() + "/" + (curdate.getMonth() + 1) + "/" + curdate.getFullYear();
如果问题是输入,您可以通过拆分获取日期,月份和年份,并使用“新日期(年,月,日,小时,分钟,秒,毫秒)创建日期”:
var input = "26/05/1984";
var aux = input.split("/");
var day =aux[0];
var month = aux[1];
var year = aux[2];
var date = new Date(year,month - 1,day,0,0,0,0);