Javascript 在javascript中从当前日期找出上一年的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11649672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find out the previous year date from current date in javascript
提问by user1519718
I need to find out the previous year date from current date and then set as minDate in jQuery UIdatepicker in javascript
我需要从当前日期找出上一年的日期,然后在 javascript 中的 jQuery UIdatepicker 中设置为 minDate
My date formaqt is dd-mm-yy
我的日期格式是 dd-mm-yy
Ie
IE
- current date is
25-07-2012
- I need to get
25-07-2011
- 当前日期是
25-07-2012
- 我需要得到
25-07-2011
回答by Dor Cohen
You need to use getFullYear()
你需要使用 getFullYear()
and then generate new Date
然后生成新的日期
var d = new Date(2012, 7, 25);
d.setFullYear(d.getFullYear() - 1);
回答by Sharmila
Try this
尝试这个
var d = new Date();
var pastYear = d.getFullYear() - 1;
d.setFullYear(pastYear);
console.log(d);
回答by Sahil Ralkar
this solution will help.
这个解决方案会有所帮助。
new Date(new Date().setFullYear(new Date().getFullYear() - 1));
回答by André Silva
Use this :
用这个 :
var date = new Date();
var intYear = date.getFullYear() - 1;
回答by Barmar
Datepicker allows you to put a number as the minDate
option, and it uses that as an offset from the current date. So you can write:
Datepicker 允许您将一个数字作为minDate
选项,并将其用作与当前日期的偏移量。所以你可以写:
minDate: -365
to specify 1 year ago. This doesn't take leap years into account, though.
指定 1 年前。不过,这并没有考虑闰年。
回答by Bergi
For strings:
对于字符串:
curdate.substr(0, 6)+(curdate.substr(6)-1);
If you'd use a Date
object, you could easily subtract a year with the set[Full]Year
method.
如果您使用Date
object,则可以轻松地使用该set[Full]Year
方法减去一年。
回答by Bergi
To avoid the Date object (if that is what OP wishes):
为了避免 Date 对象(如果这是 OP 希望的):
var currDate = '25-07-2012';
var dateParts = currDate.split('-');
dateParts[2] = parseInt(dateParts[2], 10) - 1;
alert(dateParts.join('-'));?
回答by user4193024
var today = new Date();
var curyear = today.getFullYear();
var curyearMonth = today.getMonth() + 1;
var curyearDay = today.getDate();
var lastYear = curyear - 1;
if ((curyearMonth == 2) && (curyearDay == 29)) {
curyearDay = 28;
}
var lastYearDisplay = ("0000" + lastYear.toString()).slice(-4) + "-" + ("00" + curyearMonth.toString()).slice(-2) + "-" + ("00" + curyearDay.toString()).slice(-2);
alert("LastWeekDate : " + lastYearDisplay);
回答by fidelity
var date = CALC.today(); //(function in my dev-enviro - gives today's date)
var year = formatDate(date, 'yyyy'); //year is 2016
year = parseInt(year); //make sure '2016' becomes an integer
year = year -1; // var year is now: 2015.
//its been a while, but that's what I did from memory.
回答by N.Tayel
function getTodayDate() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is not 0!
var yyyy = today.getFullYear();
if (dd < 10) { dd = '0' + dd }
if (mm < 10) { mm = '0' + mm }
today = yyyy + '-' + mm + '-' + dd;
return today;
};
function getYearAgo(){
var lastYear = new Date();
var dd = lastYear.getDate();
var mm = lastYear.getMonth() + 1; //January is not 0!
var yyyy = lastYear.getFullYear(getTodayDate) - 1;
if (dd < 10) { dd = '0' + dd }
if (mm < 10) { mm = '0' + mm }
lastYear = yyyy + '-' + mm + '-' + dd;
return lastYear;
}