0

Updated My Question

How to get total minutes of difference between two dates using pure JavaScript when

Condition (1):: Same month, same year but date changes

newDate: 18/10/2016 0:50
oldDate: 17/10/2016 23:05

Condition (2):: Last date of current month and 1st date of next month

newDate: 1/11/2016 0:50  
oldDate: 31/10/2016 23:05

Condition (3):: Last date of year and 1st date of new year

newDate: 1/1/2017 0:50  
oldDate: 31/12/2016 23:05

Note: Please have a look newDate and oldDate to understand the conditions.

Thanks

Kirankumar Dafda
  • 2,354
  • 5
  • 29
  • 56

2 Answers2

0

Completed My Requirements with the below pure JavaScript code

In my code starttime and endtime are

//var startTime = localStorage.getItem("starttime");
//var endTime = new Date();

Example Here.

var startTime = new Date("Sat Dec 31 2016 15:35:57 GMT+0530 (India Standard Time)");
var endTime = new Date("Sun Jan 1 2017 15:35:57 GMT+0530 (India Standard Time)");

var totalMiliseconds = endTime - startTime;
alert(totalMiliseconds);
//output:: 86400000

var totalSeconds = totalMiliseconds/1000;
alert(totalSeconds);
//output:: 86400

var totalMinuts = totalSeconds/60;
alert(totalMinuts);
//output:: 1440

var totalHours = totalMinuts/60;
alert(totalHours);
//output:: 24

And this fulfill my all 3 conditions.

Thank You For Your Support !!!

Kirankumar Dafda
  • 2,354
  • 5
  • 29
  • 56
  • Do not parse strings with the Date constructor (or Date.parse, they are equivalent for parsing). Always use a parse function or library. – RobG Oct 19 '16 at 01:49
  • I have stored start time and date into localstorage so while reusing the same date I need to parse it with date constructor. – Kirankumar Dafda Oct 19 '16 at 10:48
  • 1
    You do not **need** to use the Date constructor, it's a poor choice for parsing. Use a simple parse function or one of the many good parsing and formatting libraries like [*fecha.js*](https://github.com/taylorhakes/fecha) or [*moment.js*](http://momentjs.com). – RobG Oct 20 '16 at 03:29
  • but as I have mentioned before that already I have added many js libraries like scrollbox.js touchswipe.js and other so do not wants to add one more. Thanks btw @RobG – Kirankumar Dafda Oct 20 '16 at 04:53
  • If you don't want to use a library (fecha.js is just 2k), write a simple function to parse your particular format. – RobG Oct 20 '16 at 23:26
0

Since you don't want to use a library for parsing date strings, you can write a simple function such as:

// Parse date string in "Sat Dec 31 2016 15:35:57 GMT+0530 (India Standard Time)" format
function parseDate(s) {
  // Split into tokens
  var b = s.match(/\w+/g) || [];
  var months = 'jan feb mar apr may jun jul aug sep oct nov dec'.split(' ');
  // Determine offset in minutes
  var offSign = /GMT+/.test(s)? -1 : 1;
  var offset = b[8].substr(0,2)*60 + +b[8].substr(2,2);
  // Create date, applying offset to minutes
  var date = new Date(Date.UTC(b[3],
             months.indexOf(b[1].toLowerCase()), 
             b[2], 
             b[4],
             +b[5] + (offSign*offset),
             b[6]));
  return date;
}

var d = parseDate("Sat Dec 31 2016 15:35:57 GMT+0530 (India Standard Time)")
console.log('UTC: ' + d.toISOString() + '\n' +
            'Local: ' + d.toLocaleString());
RobG
  • 142,382
  • 31
  • 172
  • 209
  • Great !!! Now can you please explain how do i get total difference of time by dividing new date minus old date ? – Kirankumar Dafda Oct 21 '16 at 05:15
  • 1
    You can get the difference between two dates in milliseconds simply by subtracting one from the other, e.g. for one day: `new Date(2016,0,2) - new Date(2016,0,1)`, there are [*many questions about that already*](http://stackoverflow.com/search?q=%5Bjavascript%5D+difference+between+dates). ;-) – RobG Oct 21 '16 at 05:53
  • This is much better, I haven't tried your code in my project but I appreciate your answer and replies. – Kirankumar Dafda Oct 21 '16 at 10:10