How to Subtract a Day from a Date

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());

How to Subtract Days From Date in TypeScript

getDate returns the date of the month (1-31), so creating a new Date from it treats that number as "milliseconds since epoch".

What you probably want is to use setDate to change the date as it automatically handled going backwards through months/years.

protected generateLastWorkingDay(): Date {
const lastWorkingDay = new Date();

while(!this.isWorkingDay(lastWorkingDay)) {
lastWorkingDay.setDate(lastWorkingDay.getDate()-1);
}

return lastWorkingDay;
}

private isWorkingDay(date: Date) {
const day = date.getDay();

const isWeekday = (day > 0 && day < 6);

return isWeekday; // && !isPublicHoliday?
}

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()));

from Date I just want to subtract 1 day in javascript/angularjs

You need to specify what amount of time you are subtracting. At the moment its 1, but 1 what? Therefore, try getting the days using getDate() and then subtract from that and then set the date with setDate().

E.g.

var temp = new Date("October 13, 2014 22:34:17");
temp.setDate(temp.getDate()-1);

How to subtract n days from date in Typescript

You can do it by Date.prototype.setDate.

let d = new Date("2020-01-24");

d.setDate(d.getDate() - 5); // subtract 5 days

console.log(d);

console.log(d.toISOString().split("T")[0]); // 2020-01-19

Subtract one day from datetime

Try this

SELECT DATEDIFF(DAY,  DATEADD(day, -1, '2013-03-13 00:00:00.000'), GETDATE())

OR

SELECT DATEDIFF(DAY,  DATEADD(day, -1, @CreatedDate), GETDATE())

How to subtract a day from a date?

You can use a timedelta object:

from datetime import datetime, timedelta

d = datetime.today() - timedelta(days=days_to_subtract)

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.



Related Topics



Leave a reply



Submit