简体   繁体   English

新的Date(date).getTime(); 以d / m / y格式读取时间

[英]new Date(date).getTime(); to read time in d/m/y format

EDIT: I created an example of what I am trying to do: http://plnkr.co/edit/HvItQP2uDooaMgK7GRQe?p=preview 编辑:我创建了一个我想做的例子: http : //plnkr.co/edit/HvItQP2uDooaMgK7GRQe?p=preview

There are several problems with it. 它有几个问题。

  1. It does not work in FF (on MAC). 它不适用于FF(在MAC上)。 No errors that I can see. 我看不到任何错误。

  2. In chrome after selecting 11-04-17 to 17-04-17 It still shows rows with 30/03/2017 在Chrome中选择11年11月17日至17年4月17日后,它仍显示2017年3月30日的行

I really appreciate everyone's help and again please keep in mind I just started with JS last week. 我非常感谢大家的帮助,请再次记住我上周刚开始使用JS。 I can read the code and (possibly) understand the logic, but I am not on the level where I can clearly see how to implement it. 我可以阅读代码并(可能)理解其逻辑,但是我还没有清楚地知道如何实现它。


I am using datatables to sort some basic data by dates. 我正在使用数据表按日期对一些基本数据进行排序。 There was an excellent example here: that I copied. 这里有一个很好的例子我复制了。 The example dates are in mm/dd/yyyy format. 日期示例为mm / dd / yyyy格式。

I need it to run in dd/mm/yyyy. 我需要它以dd / mm / yyyy运行。 When I switch the dates in the example to the format I need it no longer works. 当我将示例中的日期切换为所需的格式时,它将不再起作用。

I am very new to JS so I imagine this is the part that does the magic: 我对JS非常陌生,所以我想这是实现魔术的部分:

 minDateFilter = new Date(date).getTime();

minDateFilter grabs the data in the input, and .getTime() converts it to seconds from 1970. I googled around if I can get it to start with day something like .getTime(dateFormat: "dd-mm-yyyy") but no luck yet. minDateFilter抓取输入中的数据,然后.getTime()将其转换为1970年的秒数。然而。

If anyone could help me out here I'd appreciate it. 如果有人可以在这里帮助我,我将不胜感激。 I can always use a hidden field but rather not. 我总是可以使用隐藏字段,但是不能。

Thank you 谢谢

getTime returns unix timestamp in seconds, not date like 17/04/2017 . getTime以秒为单位返回unix时间戳,而不是类似17/04/2017日期。 You will have to use toLocalDateString . 您将必须使用toLocalDateString

 var date = new Date(); console.log(date.toLocaleDateString("en-GB")); 

d = new Date(date);
f=d=>(d+"").length==2?""+d:"0"+d;
minDateFilter= f(d.getDate())+"-"+f(d.getMonth()+1)+"-"+f(d.getYear()%100);

http://jsbin.com/sosurohaje/edit?console http://jsbin.com/sosurohaje/edit?console

Explanation: 说明:

d.getDate returns the day of the month, d.getDate返回每月的某天,

d.getMonth returns the month from 0-11 (thats why the +1 ) d.getMonth返回0-11的月份(这就是+1的原因)

.getYear returns the year from 1900, thats why i used modulo 100 to format it: .getYear返回从1900年开始的年份,这就是为什么我使用模100对其进行格式化的原因:

2014 => 114 %100 => 14
1914 => 14 %100 => 14
2000 => 100 % 100 => 0

And thanks to oen44 ive included a function f that will add a leading 0 if it isnt a two digit number... 还要感谢oen44 ive包含了一个函数f ,如果它不是两位数,它将添加前导0。

I would String#split the string and then parse that into Date . 我将String#split字符串,然后将其解析为Date Depending on UTC you will get a different value in ms back (unless you happen to be a GMT timezone). 根据UTC,您将获得不同的毫秒值(除非您碰巧是GMT时区)。 In your case where you are just using this for the sorting reference, it should not matter which value you use, as long as you use the same type for all of them. 在您仅将其用作排序参考的情况下,使用哪个值都无所谓,只要对所有值使用相同的类型即可。

 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)); 

Update: This is beyond your actual question, and as such is no longer on topic. 更新:这超出了您的实际问题,因此不再是主题。 However, answer merged with your code, no warranty. 但是,答案与您的代码合并,不提供任何保证。 There are also plugins available, which include moments , if you don't want to do this yourself. 如果您不想自己做,还可以使用一些插件,其中包括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> 

If you have the date Object and you know the output format you can construct the output string with the date methods, in example: 如果您拥有日期对象,并且知道输出格式,则可以使用date方法构造输出字符串,例如:

var date = new Date("05/26/1984");
var formatDate = curdate.getDate() + "/" + (curdate.getMonth() + 1) + "/" + curdate.getFullYear();

If the problem is the input you can get the day, month and year with split and create de date with "new Date(year, month, day, hours, minutes, seconds, milliseconds)": 如果问题是输入,则可以拆分并获取日期,月份和年份,并使用“新日期(年份,月份,日期,小时,分钟,秒,毫秒)”创建日期:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM