20597

JavaScript date difference Days [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/18623783/get-the-time-difference-between-two-datetimes" dir="ltr" rel="nofollow">Get the time difference between two datetimes</a> <span class="question-originals-answer-count"> 12 answers </span> </li> </ul>

I want to calculate the difference between two dates. Currently, I am doing the code bellow:

function calculDiff(){ var v1= $('#date_debut').val(); var v2=$('#date_fin').val(); var dayDiff=((v2-v1) / (24*60*60*1000)); var dayDiff2=Math.floor(dayDiff); console.log(v1 + ' '+ v2); console.log(dayDiff + ' '+ dayDiff2); } $(document).ready(function(){ $('#date_debut').datepicker().bind('change paste keyup',function(){ calculDiff(); return false; }); // $('#date_fin').datepicker().bind('change paste keyup',function(){ calculDiff(); return false; }); });

PS:I have 2 input type date (date_debut and date_fin)

Answer1:

Try this

var now = "04/09/2013 15:00:00"; var then = "04/09/2013 14:20:30"; moment.utc(moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss")

See more in <a href="http://momentjs.com/docs/#/displaying/difference/" rel="nofollow">http://momentjs.com/docs/#/displaying/difference/</a>

Answer2:

I think you want day difference, you can user below code to get days difference.

var start_date = new Date($('#date_debut').val()); var end_date = new Date($('#date_fin').val()); var diff = new Date(end_date - start_date); var days = diff/1000/60/60/24; console.log("diff=>"+days);

When user selected first date open other date datepicker to select remaining field, and then calculate difference.

Recommend

  • How to order dataframe for plotting 3d bar in pandas
  • calculate minutes left between hours actionscript 3
  • Oracle SQL - Generate and update a column with different random number in every row
  • Transpose table then set and rename index
  • Correctly Importing Apache Commons Math Package
  • ConfigurationBuilder not working in azure function
  • Unable to play media with vlc ocx
  • How to set elevation color?
  • C# fibonacci function returning errors
  • How to getText() from the input field of an angularjs Application
  • Overlapping controls in Windows XP
  • android google indoor map
  • Run multiple queries from 1 SQL file showing result in multiple tables
  • D3 get axis values on zoom event
  • MySQL Order by column = x, column asc?
  • Detect when Facebook like button is clicked
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • Ensure fsync did its job
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • Xcode 4 NSLog Macro link in Xcode 3
  • Magento Fatal error: Maximum execution error solution, on WAMP
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • How to set ini file attributes during an Inno install
  • Yii2: Config params vs. const/define
  • Python CGI os.system causing malformed header
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Date difference with leap year
  • recyclerView does not call the onBindViewHolder when scroll in the view
  • WinForms: two way TextBox problem
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Upload files with Ajax and Jquery
  • Calling of Constructors in a Java
  • How to pass list parameters for each object using Spring MVC?
  • Traverse Array and Display in markup
  • Transpose CSV data with awk (pivot transformation)
  • Buffer size for converting unsigned long to string
  • AngularJs get employee from factory
  • Proper way to use connect-multiparty with express.js?
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Converting MP3 duration time