Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I subtract one week from this date in jquery?

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

like image 495
Infinity Avatar asked Dec 13 '11 12:12

Infinity


People also ask

How do you minus days from date in JS?

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.

How do you subtract a day from a date?

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.

Can you subtract dates in JavaScript?

Use the Math. abs() Function to Subtract Datetime in JavaScript.

How do I subtract days from a date in typescript?

let yesterday=new Date(new Date(). getTime() - (1 * 24 * 60 * 60 * 1000)); let last3days=new Date(new Date(). getTime() - (3 * 24 * 60 * 60 * 1000));


2 Answers

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.

like image 197
David Hedlund Avatar answered Oct 06 '22 09:10

David Hedlund


I'd do something like

var myDate = new Date(); var newDate = new Date(myDate.getTime() - (60*60*24*7*1000)); 
like image 45
Jean-Philippe Gire Avatar answered Oct 06 '22 08:10

Jean-Philippe Gire