How to Get the 30 Days Before Date from Todays Date

How to get 30 days prior to current date?

Try using the excellent Datejs JavaScript date library (the original is no longer maintained so you may be interested in this actively maintained fork instead):

Date.today().add(-30).days(); // or...
Date.today().add({days:-30});

[Edit]

See also the excellent Moment.js JavaScript date library:

moment().subtract(30, 'days'); // or...
moment().add(-30, 'days');

Today's date -30 days in JavaScript

setDate() doesn't return a Date object, it returns the number of milliseconds since 1 January 1970 00:00:00 UTC. You need separate calls:

var date = new Date();
date.setDate(date.getDate() - 30);
var dateString = date.toISOString().split('T')[0]; // "2016-06-08"

Javascript get date 30 days ago

You'll instead want to use:

var priordate = new Date(new Date().setDate(beforedate.getDate()-30));

if you want it on one line. By using:

new Date().setDate(beforedate.getDate()-30);

you're returning the time since epoch (a number, not a date) and assigning it to priordate, but it's not a Date anymore and so does not have the getDate function.

how to get the 30 days before date from Todays Date

T-SQL

declare @thirtydaysago datetime
declare @now datetime
set @now = getdate()
set @thirtydaysago = dateadd(day,-30,@now)

select @now, @thirtydaysago

or more simply

select dateadd(day, -30, getdate())

(DATEADD on BOL/MSDN)

MYSQL

SELECT DATE_ADD(NOW(), INTERVAL -30 DAY)

( more DATE_ADD examples on ElectricToolbox.com)

T-SQL query to get a date +30 days from today exactly

To calculate today plus 30 days as a Date you can use:

DateAdd( day, 30, Cast( GetDate() as Date ) )

Note that if you are trying to retrieve rows by a DateTime column that have that date you would use:

where DateAdd( day, 30, Cast( GetDate() as Date ) ) <= DateTimeColumn and
DateTimeColumn < DateAdd( day, 31, Cast( GetDate() as Date ) )

This will include all times up to, but not including, midnight starting the following day. The condition is SARGABLE so that it can benefit from indexes.

Add 30 days to a Current date - JS

var date = new Date(); // Now
date.setDate(date.getDate() + 30); // Set now + 30 days as the new date
console.log(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 30 days from the current date using SQL Server

You can convert it to datetime, and then use DATEADD(DAY, -30, date).

See here.

edit

I suspect many people are finding this question because they want to substract from current date (as is the title of the question, but not what OP intended). The comment of munyul below answers that question more specifically. Since comments are considered ethereal (may be deleted at any given point), I'll repeat it here:

DATEADD(DAY, -30, GETDATE())


Related Topics



Leave a reply



Submit