this is my code
var myDate = new Date(); todaysDate = ((myDate.getDate()) + '/' + (myDate.getMonth()) + '/' + (myDate.getFullYear())); $('#txtEndDate').val(todaysDate);
I need txtEndDate's value = today's date - one week
To subtract days to a JavaScript Date object, use the setDate() method. Under that, get the current days and subtract days. JavaScript date setDate() method sets the day of the month for a specified date according to local time.
Therefore, you can add or subtract days as easy as adding or minus the number of days in Excel. 1. Select a blank cell you will place the calculating result, type the formula =A2+10, and press the Enter key. Note: For subtracting 10 days from the date, please use this formula =A2–10.
Use the Math. abs() Function to Subtract Datetime in JavaScript.
let yesterday=new Date(new Date(). getTime() - (1 * 24 * 60 * 60 * 1000)); let last3days=new Date(new Date(). getTime() - (3 * 24 * 60 * 60 * 1000));
You can modify a date using setDate
. It automatically corrects for shifting to new months/years etc.
var oneWeekAgo = new Date(); oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
And then go ahead to render the date to a string in any matter you prefer.
I'd do something like
var myDate = new Date(); var newDate = new Date(myDate.getTime() - (60*60*24*7*1000));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With