[英]Compare two dates with different format Javascript
目前,我正在使用JavaScript项目(使用节点聊天应用程序)。 我必须显示时间和日期以及聊天消息。 如果今天收到消息,则只需要显示当前时间,如果前几天收到消息,则需要显示日期和时间。 我当前的问题是,如何比较当前日期和消息接收日期。 为了获得当前日期,我使用了bleow函数
var dt = new Date();
上面的代码返回当前日期为
2017年5月4日星期四10:27:12 GMT + 0530(IST)
但是从mySql db获得的消息日期类似于:
2017-05-04T04:26:37.000Z
我没有任何规定要告诉后端开发人员更改其发送日期的格式。 那么,现在该怎么办? 我要做的就是这样
if(dt>historyDate){
// print the time only
}
else{
//print date and time
}
在上面的代码中,dt是当前日期和时间,historyDate是从数据库发送的日期和时间。 那么如何比较两个日期呢? 如果需要更多详细信息,请发表评论。
只需使用Date.parse即可返回自1970年1月1日以来的日期(以毫秒为单位)
var t1 = Date.parse("2017-05-04T04:26:37.000Z") // returns 1493871997000 console.log(t1); var t2 = Date.parse("Thu May 04 2017 10:27:12 GMT+0530") // returns 1493873832000 console.log(t2);
然后,您可以继续比较这两个值,因为它们都是数字。
我已经创建了一个用于发布时间查找的JavaScript函数。它将有助于查找您的不同之处。它们将发现与发布时间和当前时间不同的地方。 getTime()
获取毫秒数,然后传递timecheck()
,它将发现差异
var d = new Date("2017-05-04T04:26:37.000Z") console.log(timecheck(d.getTime())) function timecheck(data) { data = Number(data); var m = ['Jan', 'Feb', 'Mar', 'Aprl', 'May', 'Jun', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; var w = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; var res = ""; var d = Date.now(); var c = d - data; x = c / 1000; seconds = Math.floor(x % 60); x /= 60 minutes = Math.floor(x % 60); x /= 60 hours = Math.floor(x % 24); x /= 24 days = Math.floor(x); if (days > 0) { res = ' yesterday'; if (days > 1) { var n = new Date(data); res = m[n.getMonth()] + ' ' + n.getDate() + ' ' + n.getFullYear() + ' ' + w[n.getDay()]; } } else if (hours > 0) { res = hours + ' hours ago'; } else if (minutes > 0) { res = minutes + ' minutes ago'; } else if (seconds > 0) { res = seconds + ' seconds ago'; } else { res = 'Justnow' } return 'Posted ' + res; }
2017-05-04T04:26:37.000Z采用已知和公认的格式。 因此,您需要做的是:
var otherDt = new Date("2017-05-04T04:26:37.000Z");
var today = new Date();
if(today.getTime() > otherDt.getTime()){
// print the time only
}
else{
//print date and time
}
这将解决您的问题
var x= "2017-05-04T04:26:37.000Z"; // your received date string
var currentDate = new Date();
var historyDate = new Date(x)
var historyDateNoTime = historyDate.setHours(0,0,0,0);
var currentDateNoTime = currentDate.setHours(0,0,0,0);
if(currentDateNoTime === historyDateNoTime )
{
console.log(historyDate.getTime());
}
else if(currentDateNoTime > historyDateNoTime )
{
console.log(historyDate);
}
您可以使用Date.parse()
传递日期字符串,并获取以毫秒为单位的日期表示形式。 然后比较两个解析的日期。
var dt = Date.parse("Thu May 04 2017 10:27:12 GMT+0530 (IST)"); var fromDB = Date.parse("2017-05-04T04:26:37.000Z"); if (dt > fromDB) { console.log("dt is greater than date from database"); } else { console.log("database date is greater than dt"); }
在第一个解析日期,您已经使用Date.parse()方法从服务器(SQL日期)获得了。 然后创建将服务于一天中开始时间的日期变量,并使用dateObj.setHourse(0,0,0,0)设置所需时间(当前时间开始)。 然后,只需从dayBeginning获得毫秒数,然后将其与从服务器获得的时间进行比较即可。
const element = document.getElementById('received-time'); const timeString = "2017-05-04T04:26:37.000Z"; const time = Date.parse(timeString); const dayBeginning = new Date(); dayBeginning.setHours(0, 0, 0, 0); const dayBeginningTime = dayBeginning.getTime(); if (time > dayBeginning) { element.innerHTML = 'today'; } else { element.innerHTML = 'not today' }
<div id="received-time"> </div>
先取当前日期
var currentDate=new Date();
我们已经有聊天消息的日期,请将其格式化为上面的类型。 为此,我们使用
var date1=new Date(historyDate);
现在我们具有相同格式的当前日期和historyDate
然后从每个日期获取年,月和日
var date2=date1.getDate() + "-" + (date1.getMonth()+1) + "-" + date1.getFullYear();
var date3=currentDate.getDate() + "-" + (currentDate.getMonth()+1) + "-" + currentDate.getFullYear();
现在我们得到答案(示例)
date2 = 4-5-2017 date3 = 2-5-2017
现在我们可以像比较bleow一样比较date2和date3
if(date2==date3){
//print current time only
}
else{
//print date along with time
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.