How to add/subtract dates with JavaScript?
Code:
var date = new Date('2011', '01', '02');alert('the original date is ' + date);var newdate = new Date(date);
newdate.setDate(newdate.getDate() - 7); // minus the date
var nd = new Date(newdate);alert('the new date is ' + nd);
How to subtract days from a plain Date?
Try something like this:
var d = new Date();
d.setDate(d.getDate()-5);
Note that this modifies the date object and returns the time value of the updated date.
var d = new Date();
document.write('Today is: ' + d.toLocaleString());
d.setDate(d.getDate() - 5);
document.write('<br>5 days ago was: ' + d.toLocaleString());
Subtract days, months, years from a date in JavaScript
You are simply reducing the values from a number. So substracting 6 from 3 (date) will return -3 only.
You need to individually add/remove unit of time in date object
var date = new Date();
date.setDate( date.getDate() - 6 );
date.setFullYear( date.getFullYear() - 1 );
$("#searchDateFrom").val((date.getMonth() ) + '/' + (date.getDate()) + '/' + (date.getFullYear()));
How to subtract date/time in JavaScript?
This will give you the difference between two dates, in milliseconds
var diff = Math.abs(date1 - date2);
In your example, it'd be
var diff = Math.abs(new Date() - compareDate);
You need to make sure that compareDate
is a valid Date
object.
Something like this will probably work for you
var diff = Math.abs(new Date() - new Date(dateStr.replace(/-/g,'/')));
i.e. turning "2011-02-07 15:13:06"
into new Date('2011/02/07 15:13:06')
, which is a format the Date
constructor can comprehend.
How to add/subtract dates on button click?
Created a child component to manage date
state of each object in rowData
array. I have updated your code. Use it as an example to improve your code. (please refer the link here)
Format date and Subtract days using Moment.js
You have multiple oddities happening. The first has been edited in your post, but it had to do with the order that the methods were being called.
.format
returns a string. String does not have a subtract
method.
The second issue is that you are subtracting the day, but not actually saving that as a variable.
Your code, then, should look like:
var startdate = moment();
startdate = startdate.subtract(1, "days");
startdate = startdate.format("DD-MM-YYYY");
However, you can chain this together; this would look like:
var startdate = moment().subtract(1, "days").format("DD-MM-YYYY");
The difference is that we're setting startdate to the changes that you're doing on startdate, because moment is destructive.
Get difference between 2 dates in JavaScript?
Here is one way:
const date1 = new Date('7/13/2010');const date2 = new Date('12/15/2010');const diffTime = Math.abs(date2 - date1);const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); console.log(diffTime + " milliseconds");console.log(diffDays + " days");
Related Topics
Proper Use of Const for Defining Functions
How to Pass Parameters in Computed Properties in Vue.Js
How Is JavaScript Single Threaded
Correct Way to Handle Conditional Styling in React
Understanding Ajax Cors and Security Considerations
How to Send an Ajax Request on a Different Port with Jquery
Scriptmanager.Registerstartupscript Code Not Working - Why
When to Use Jsx.Element VS Reactnode VS Reactelement
Using Chrome, How to Find to Which Events Are Bound to an Element
How to Clone a JavaScript Object Except for One Key
How to Reset <Input Type = "File">
How to Filter JSON Data in JavaScript or Jquery
Window.Onunload Is Not Working Properly in Chrome Browser. Can Any One Help Me
Finding Element's Position Relative to the Document
Sum of Two Numbers with Prompt
Create an Instance of a Class in Es6 with a Dynamic Name
Does Using $This Instead of $(This) Provide a Performance Enhancement