1

I'm having hard time to get the right solution for this, so I need your help now.

date1 = "03/02/2012 09:51 pm"

date2 = "03/04/2012 06:00 pm"

Calculate the above example date into how many days diff. using JavaScript.

hodl
  • 1,420
  • 12
  • 21

3 Answers3

2
var date1 = Date.parse("03/02/2012 09:51 pm");
var date2 = Date.parse("03/04/2012 06:00 pm");

var dayDiff = (date2 / (1000*60*60*24)) - (date1 / (1000*60*60*24));
P. Galbraith
  • 2,477
  • 21
  • 24
  • 3
    When using this, be careful about where your date strings come from; if a US-localized machine generates the string and a UK-localised machine parses it, there will probably be a mismatch. US is month/day/year and UK is day/month/year. If the input is a user, you'll need to take their locale into account. – Phil H Mar 02 '12 at 10:45
0

momentjs comes handy in such cases. You can run attached snippet to see results. It allows you to extract time difference easily. Also, you can work using several cultures and languages.

moment.locale('en');

var from = moment("2016-04-14 23:30");
var until = moment("2016-04-14 23:45");
var untilDay = moment("2016-04-15 03:45");
var untilMonth = moment("2016-03-14 06:33");

$('body').append(from.to(until) + "<br>");
$('body').append(from.to(untilDay) + "<br>");
$('body').append(from.to(untilMonth) + "<br>");
<!DOCTYPE html>
<html>

<head>
  <script src="https://cdn.jsdelivr.net/momentjs/2.10.6/moment-with-locales.min.js"></script>
  <script src="https://cdn.jsdelivr.net/momentjs/2.10.6/locales.min.js"></script>
  <script src="https://code.jquery.com/jquery-1.11.3.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>

<body>

</body>

</html>
-1

Remove the original post as to modify and inflect the proper answer

var date1 = new Date("2012-03-02");
var date2 = new Date("2012-04-15");
var daysDifference = new Date(date2 - date1);
var daysDifference = daysDifference/1000/60/60/24;
document.write(daysDifference);

Try it yourself.

Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
  • 1
    You only calculate the date. What if I will change the month? – hodl Mar 02 '12 at 10:35
  • You said days difference. I gave you a quick and dirty example. If you don't formulate your questions properly, people aren't going to be able to give you the answer that you want. I'll redo my answer. – Ohgodwhy Mar 02 '12 at 10:43
  • 1
    Between the first of March and the first of April, 31 days go by. The question is well formulated and your solution fails at it (and also with the issues @PhilH stated). – paradoja Mar 02 '12 at 10:49
  • I provided him exactly what he asked for. He did not state that the days would change dynamically. I don't make assumptions about questions people make. I answer exactly as they've requested. He asked for a script that would calculate the difference in days between two days. He didn't specify if they were dynamic, their locale, formatting, or any of the like. If you go to the store to buy a bag of Wheat Bread, and you buy that bread. Would you expect the cashier to say, 'Well do you need any Meat? Cheese? What about Condiments?'. This is what I meant by formulating your question well. – Ohgodwhy Mar 02 '12 at 10:57