0

Currently I am working with a JavaScript project (chat application using node). I have to display the time and date along with chat message. If the message is received on today, only needs to show the current time and if the message was received on previous days, need to display date along with time. My current problem is that, how can I compare the current date with message received date. For getting the current date I have used the bleow function

var dt = new Date();

The above code returns current date as

Thu May 04 2017 10:27:12 GMT+0530 (IST)

But the date obtained from mySql db for the message is like:

2017-05-04T04:26:37.000Z

I dont have any provision to told back-end developers to change the format of date they send. So now what can I do? The way I am going to do is like this

if(dt>historyDate){
   // print the time only
   }
else{
   //print date and time
}

In the above code, dt is the current date and time and historyDate is date and time send from DB. So how can I compare the two dates? If any more details need, please comment.

Sulu.MeanStack
  • 299
  • 2
  • 3
  • 18
  • Do you need time?? – Jins Peter May 04 '17 at 05:35
  • no, just need to know which date is higher. – Sulu.MeanStack May 04 '17 at 05:36
  • 1
    You need to parse both the dates, convert to their millisecond value by doing `dateObj.getTime()` and them compare them. – gurvinder372 May 04 '17 at 05:37
  • I think about it, but the problem is take an example, I send one message at 9.10AM today take time as 'x' and send another message at 10.10PM take it as 'y'. If I get the millisecond by using getTime() function I get y as greater. But no use , I need the Date to be compared. Please read the question once – Sulu.MeanStack May 04 '17 at 05:41

7 Answers7

2

Just use Date.parse It returns the date in milliseconds since Jan 1, 1970

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

Then you can go ahead and compare the two values as both are numeric.

Prateek Arora
  • 274
  • 2
  • 6
  • The OP doesn't need to parse "Thu May 04 2017 10:27:12 GMT+0530", just the ISO 8601 form. You should include the usual caveat for parsing dates: [*Why does Date.parse give incorrect results?*](http://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG May 04 '17 at 06:26
0

Already i was created one JavaScript function for posting time find .It will help to find your differents .They will find different from posted time and current time .getTime() the get the millisecond then pass with timecheck() .It will find the difference

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;

}
prasanth
  • 22,145
  • 4
  • 29
  • 53
0

2017-05-04T04:26:37.000Z is in a known and accepted format. So all you need to do is:

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
 }
dev8080
  • 3,950
  • 1
  • 12
  • 18
0

This will solve your problem

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

    }   
Jins Peter
  • 2,368
  • 1
  • 18
  • 37
0

You can use Date.parse() to pass the date string in and get a date representation in milliseconds. Then compare the two parsed dates.

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");
}
Kyle Richardson
  • 5,567
  • 3
  • 17
  • 40
0

At first parse date you have got from server (sql date) with Date.parse() method. Then create date variable that will serve time of the beginning of the day and set needed time (beginning of current time) with dateObj.setHourse(0, 0, 0, 0). Then just get milliseconds from dayBeginning and compare it with time you have got from server.

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>
0

First take the current date

var currentDate=new Date();

we already have date of chat message, format that to to above type. For that we use

var date1=new Date(historyDate);

Now we have current date and historyDate in same format

Then get year,month and day from each date

var date2=date1.getDate()  + "-" + (date1.getMonth()+1) + "-" + date1.getFullYear();
 var date3=currentDate.getDate()  + "-" + (currentDate.getMonth()+1) + "-" + currentDate.getFullYear();

now we get answer (example)

date2 = 4-5-2017 date3 = 2-5-2017

now we can compare date2 and date3 as like bleow

if(date2==date3){
    //print current time only
}
else{
   //print date along with time
}
Sulu.MeanStack
  • 299
  • 2
  • 3
  • 18