admin管理员组文章数量:1128329
I have a start calendar input box and an end calendar input box. We want defaults start calendar input box 30 days prior to current date and the end calendar input box to be the current date. Here is my date vars.
var today = new Date(),
dd = today.getDate(),
mm = today.getMonth(),
yyyy = today.getFullYear(),
month = ["January", "February", "March",
"April", "May", "June", "July", "August",
"September", "October" "November", "December"],
startdate = month[mm] + ", " + yyyy.toString();
The end date would be something like var enddate = startdate - 30;
Obviously this won't work.
So if the current date is December 30, 2011 I'd want the start date to read December 1, 2011.
EDIT: My question was answered... sort of. Date.today();
and Date.today().add(-30);
work but I need the date in the format of January 13, 2012
. Not Fri Jan 13 2012 10:48:56 GMT -055 (EST)
. Any help?
MORE EDIT: As of this writing it's 2018. Just use Moment.js. It's the best.
I have a start calendar input box and an end calendar input box. We want defaults start calendar input box 30 days prior to current date and the end calendar input box to be the current date. Here is my date vars.
var today = new Date(),
dd = today.getDate(),
mm = today.getMonth(),
yyyy = today.getFullYear(),
month = ["January", "February", "March",
"April", "May", "June", "July", "August",
"September", "October" "November", "December"],
startdate = month[mm] + ", " + yyyy.toString();
The end date would be something like var enddate = startdate - 30;
Obviously this won't work.
So if the current date is December 30, 2011 I'd want the start date to read December 1, 2011.
EDIT: My question was answered... sort of. Date.today();
and Date.today().add(-30);
work but I need the date in the format of January 13, 2012
. Not Fri Jan 13 2012 10:48:56 GMT -055 (EST)
. Any help?
MORE EDIT: As of this writing it's 2018. Just use Moment.js. It's the best.
Share Improve this question edited Sep 7, 2018 at 19:56 Jesse Atkinson asked Jan 12, 2012 at 21:38 Jesse AtkinsonJesse Atkinson 11.4k13 gold badges43 silver badges45 bronze badges 1- stackoverflow.com/questions/563406/… I think that might help. – Tim Withers Commented Jan 12, 2012 at 21:41
18 Answers
Reset to default 245To subtract days from a JS Date object you can use the setDate()
method, along with the date to start the calculation from. This will return an epoch timestamp as an integer, so to convert this to a Date you'll need to again provide it to the Date()
object constructor. The final example would look like this:
var today = new Date();
var priorDate = new Date(new Date().setDate(today.getDate() - 30));
console.log(today)
console.log(priorDate);
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');
Here's an ugly solution for you:
var date = new Date(new Date().setDate(new Date().getDate() - 30));
startDate = new Date(today.getTime() - 30*24*60*60*1000);
The .getTime()
method returns a standard JS timestamp (milliseconds since Jan 1/1970) on which you can use regular math operations, which can be fed back to the Date object directly.
Get next 30th day from today
let now = new Date()
console.log('Today: ' + now.toUTCString())
let next30days = new Date(now.setDate(now.getDate() + 30))
console.log('Next 30th day: ' + next30days.toUTCString())
Get last 30th day form today
let now = new Date()
console.log('Today: ' + now.toUTCString())
let last30days = new Date(now.setDate(now.getDate() - 30))
console.log('Last 30th day: ' + last30days.toUTCString())
Javascript can handle it without any external libraries.
var today = new Date();
var dateLimit = new Date(new Date().setDate(today.getDate() - 30));
document.write(today + "<br/>" + dateLimit)
Simple 1 liner Vanilla Javascript code :
const priorByDays = new Date(Date.now() - days * 24 * 60 * 60 * 1000)
For example:
days = 7
Assume current date = Fri Sep 18 2020 01:33:26 GMT+0530
The result would be : Fri Sep 11 2020 01:34:03 GMT+0530
The beauty of this is you can manipulate it to get result in desired type
timestamp :
Date.now() - days * 24 * 60 * 60 * 1000
ISOString:
new Date(Date.now() - 7 * 24 * 60 * 60 * 1000).toISOString()
Easy.(Using Vanilla JS)
let days=30;
this.maxDateTime = new Date(Date.now() - days * 24 * 60 * 60 * 1000);
ISOFormat ?
let days=30;
this.maxDateTime = new Date(Date.now() - days * 24 * 60 * 60 * 1000).toISOString();
let today = new Date()
let last30Days = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 30)
last30Days
will be in Date Object
I will prefer moment js
startDate = moment().subtract(30, 'days').format('LL') // January 29, 2015
endDate = moment().format('LL'); // February 28, 2015
I use date.js. It handles this easily and takes care of all the leap-year nastiness.
This is an ES6 version
let date = new Date()
let newDate = new Date(date.setDate(date.getDate()-30))
console.log(newDate.getMonth()+1 + '/' + newDate.getDate() + '/' + newDate.getFullYear() )
You can do that simply through 1 line of code using moment in Node JS. :)
let lastOneMonthDate = moment().subtract(30,"days").utc().toISOString()
Don't want UTC format, EASIER :P
let lastOneMonthDate = moment().subtract(30,"days").toISOString()
If you aren't inclined to momentjs, you can use this:
let x = new Date()
x.toISOString(x.setDate(x.getDate())).slice(0, 10)
Basically it gets the current date (numeric value of date of current month) and then sets the value. Then it converts into ISO format from which I slice down the pure numeric date (i.e. 2019-09-23)
Hope it helps someone.
For anyone looking for the format 'dd month yyyy', here's what worked for me:
let date = new Date()
let newDate = new Date(date.setDate(date.getDate()-30))
console.log(newDate.getDate()+ ' ' +newDate.toLocaleString('default', {month: 'long'}) + ' ' + newDate.getFullYear() )
Use moment.js
let startDate = moment().subtract(30, "days").format('YYYY-MM-DD'); //2021-05-18
let endDate = moment().format('YYYY-MM-DD'); //2021-06-17
let currentDate = new Date()
let pastDate = new Date(currentDate.getTime() - (30 * 24 * 60 * 60 * 1000)); // 30 days
The date-fns
library provides an easy way to accomplish this using one line.
In this case, to receive the date 30 days prior to today, you would write...
import { sub } from "date-fns";
let previousDate = sub(new Date(), { days: 60 });
...in where the first argument of the method helper is the starting date (today) and the second argument is an object, in where you can specify the following of which to subtract from the passed in date:
months
weeks
days
hours
minutes
seconds
Documentation: https://date-fns.org/v3.6.0/docs/sub
本文标签: javascriptHow to get 30 days prior to current dateStack Overflow
版权声明:本文标题:javascript - How to get 30 days prior to current date? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736720391a1949434.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论