在Javascript中将日期时间字符串转换为时间戳

问题简述:

转换date-month-year hour(24):minutetimestamp的最简单方法是什么?

由于更多的观点在顶部添加了明确的问题,所以如果需要快速帮助,无需通过背景和所有。


背景 :

我有一个简单的html表,我使用jquery排序器来排序我的表列。

一切都工作正常,除了日期列具有以下格式的数据,

 17-09-2013 10:08 date-month-year hour(24):minute 

此列按字母顺序排序,但不是我预期的排序(日期明确)。 我尝试使用自定义解析器,如下所示,

 $.tablesorter.addParser({ id: 'date_column', // my column ID is: function(s) { return false; }, format: function(s) { var timeInMillis = new Date.parse(s); return timeInMillis; }, type: 'numeric' }); 

问题:由于new Date.parse(s)而失败。

问题:date-month-year hour(24):minute为时间戳的最简单方法是什么? 然后我可以跳过var timeInMillis = new Date.parse(s); 线。

谢谢

编辑:

很抱歉有关milliseconds的混淆,实际上应该是timestamp ,它是表示当前时间和日期的数字。

解析日期是JavaScript的一个难点,因为没有广泛的原生支持。 但是,依靠Date(year, month, day [, hour, minute, second, millisecond])构造函数签名,您可以执行以下操作。

 var dateString = '17-09-2013 10:08', dateTimeParts = dateString.split(' '), timeParts = dateTimeParts[1].split(':'), dateParts = dateTimeParts[0].split('-'), date; date = new Date(dateParts[2], parseInt(dateParts[1], 10) - 1, dateParts[0], timeParts[0], timeParts[1]); console.log(date.getTime()); //1379426880000 console.log(date); //Tue Sep 17 2013 10:08:00 GMT-0400 

您还可以使用带有捕获组的正则表达式来解析一行中的日期字符串。

 var dateParts = '17-09-2013 10:08'.match(/(\d+)-(\d+)-(\d+) (\d+):(\d+)/); console.log(dateParts); // ["17-09-2013 10:08", "17", "09", "2013", "10", "08"] 

Date.parse()不是构造函数,它是一个静态方法。

所以,只需使用

 var timeInMillis = Date.parse(s); 

代替

 var timeInMillis = new Date.parse(s); 

似乎问题是日期格式。

  var d = "17-09-2013 10:08", dArr = d.split('-'), ts = new Date(dArr[1] + "-" + dArr[0] + "-" + dArr[2]).getTime(); // 1379392680000 

对于我们这些使用非ISO标准日期格式的人,例如平民白话01/01/2001(mm / dd / YYYY),包括带有am / pm标记的12小时日期格式的时间,以下函数将返回有效的Date对象:

 function convertDate(date) { // # valid js Date and time object format (YYYY-MM-DDTHH:MM:SS) var dateTimeParts = date.split(' '); // # this assumes time format has NO SPACE between time and am/pm marks. if(dateTimeParts[1].indexOf(' ') == -1 && dateTimeParts[2] === undefined) { var theTime = dateTimeParts[1]; // # strip out all except numbers and colon var ampm = theTime.replace(/[0-9:]/g,''); // # strip out all except letters (for AM/PM) var time = theTime.replace(/[[^a-zA-Z]/g,''); if(ampm == 'pm') { time = time.split(':'); // # if time is 12:00, don't add 12 if(time[0] == 12) { time = parseInt(time[0]) + ':' + time[1] + ':00'; } else { time = parseInt(time[0]) + 12 + ':' + time[1] + ':00'; } } else { // if AM time = time.split(':'); // # if AM is less than 10 o'clock, add leading zero if(time[0] < 10) { time = '0' + time[0] + ':' + time[1] + ':00'; } else { time = time[0] + ':' + time[1] + ':00'; } } } // # create a new date object from only the date part var dateObj = new Date(dateTimeParts[0]); // # add leading zero to date of the month if less than 10 var dayOfMonth = (dateObj.getDate() < 10 ? ("0"+dateObj.getDate()) : dateObj.getDate()); // # parse each date object part and put all parts together var yearMoDay = dateObj.getFullYear() + '-' + (dateObj.getMonth() + 1) + '-' + dayOfMonth; // # finally combine re-formatted date and re-formatted time! var date = new Date(yearMoDay + 'T' + time); return date; 

}

用法:

 date = convertDate('11/15/2016 2:00pm');