Answer the question
In order to leave comments, you need to log in
Date subtraction with jQuery?
Good afternoon. Help with date subtraction. Thanks for the help.
Now, when you click on the createDateBtn ID, a function is triggered that takes the start date and end date from the start_date and end_date fields of the date. these dates come already in the format 2018-05-10 and are selected by the calendar type="date" in the format 05/10/2018
So the bottom line is that you need to subtract the start_date variable from the end_date variable and display the answer in the input with the ID result_date Needed
on jQuery
Now the code is like this, it simply writes the selected date to the variables
$("#createDateBtn").click(function(){
var start_date = $("#start_date").val();
var end_date = $("#end_date").val();
});
Answer the question
In order to leave comments, you need to log in
Highly recommend https://momentjs.com/ . Handy and light weight!
$("#createDateBtn").click(function(){
var start_date = $("#start_date").val();
var end_date = $("#end_date").val();
var diff = new Date(end_date - start_date );
var days = diff/1000/60/60/24;
$("#result_date").val(days);
});
You need to parse something into a normal Date object for example, then translate into unix and subtract.
Or parse on the knee through split and fill in the Date object, then translate into unix and subtract.
Needed in JavaScript.
jQuery is just a set of functions for working with the page/dom(!)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question