I
I
id56346gera2018-05-11 15:10:50
JavaScript
id56346gera, 2018-05-11 15:10:50

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

3 answer(s)
C
coderisimo, 2018-05-11
@coderisimo

Highly recommend https://momentjs.com/ . Handy and light weight!

T
TerNik, 2018-05-11
@TerNik

$("#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);
});

A
Alexander Aksentiev, 2018-05-11
@Sanasol

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 question

Ask a Question

731 491 924 answers to any question