Does anybody know of an easy way of taking a date (e.g. Today) and going back X days?
有没有人知道一种简单的约会方式(比如今天),然后回到X日?
So, for example, if I want to calculate the date 5 days before today.
举个例子,如果我想计算5天之前的日期。
25 个解决方案
#1
587
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());
#2
50
var dateOffset = (24*60*60*1000) * 5; //5 days
var myDate = new Date();
myDate.setTime(myDate.getTime() - dateOffset);
If you're performing lots of headachy date manipulation throughout your web application, DateJS will make your life much easier:
如果您在整个web应用程序中执行大量的headachy日期操作,DateJS将使您的生活更加轻松:
http://simonwillison.net/2007/Dec/3/datejs/
http://simonwillison.net/2007/Dec/3/datejs/
#3
44
It goes something like this:
它是这样的:
var d = new Date(); // today!
var x = 5; // go back 5 days!
d.setDate(d.getDate() - x);
#4
18
I noticed that the getDays+ X doesn't work over day/month boundaries. Using getTime works as long as your date is not before 1970.
我注意到getDays+ X不能超过一天/月的边界。使用getTime工作只要你的日期不在1970年之前。
var todayDate = new Date(), weekDate = new Date();
weekDate.setTime(todayDate.getTime()-(7*24*3600000));
#5
11
I made this prototype for Date so that I could pass negative values to subtract days and positive values to add days.
我做了这个原型,以便我可以通过负值来减去天数和正值来增加天数。
if(!Date.prototype.adjustDate){
Date.prototype.adjustDate = function(days){
var date;
days = days || 0;
if(days === 0){
date = new Date( this.getTime() );
} else if(days > 0) {
date = new Date( this.getTime() );
date.setDate(date.getDate() + days);
} else {
date = new Date(
this.getFullYear(),
this.getMonth(),
this.getDate() - Math.abs(days),
this.getHours(),
this.getMinutes(),
this.getSeconds(),
this.getMilliseconds()
);
}
this.setTime(date.getTime());
return this;
};
}
So, to use it i can simply write:
因此,我可以简单地写:
var date_subtract = new Date().adjustDate(-4),
date_add = new Date().adjustDate(4);
#6
9
split your date into parts, then return a new Date with the adjusted values
将你的约会分成不同的部分,然后用调整后的值返回一个新的日期。
function DateAdd(date, type, amount){
var y = date.getFullYear(),
m = date.getMonth(),
d = date.getDate();
if(type === 'y'){
y += amount;
};
if(type === 'm'){
m += amount;
};
if(type === 'd'){
d += amount;
};
return new Date(y, m, d);
}
Remember that the months are zero based, but the days are not. ie new Date(2009, 1, 1) == 01 February 2009, new Date(2009, 1, 0) == 31 January 2009;
记住,每个月都是零,但日子不是。2009年2月1日,2009年2月1日,2009年2月1日,2009年1月31日;
#7
9
get moment.js. All the cool kids use it. It has more formatting options, etc. Where
得到moment.js。所有的酷孩子都用它。它有更多的格式化选项,等等。
var n = 5;
var dateMnsFive = moment(<your date>).subtract(n , 'day');
Optional! Convert to JS Date obj for Angular binding.
可选!转换到JS日期obj的角度绑定。
var date = new Date(dateMnsFive.toISOString());
Optional! Format
可选!格式
var date = dateMnsFive.format("YYYY-MM-DD");
#8
8
I like doing the maths in milliseconds. So use Date.now()
我喜欢用毫秒计算。所以使用Date.now()
var newDate = Date.now() + -5*24*3600*1000; // date 5 days ago in milliseconds
and if you like it formatted
如果你喜欢它的格式。
new Date(newDate).toString(); // or .toUTCString or .toISOString ...
NOTE: Date.now()
doesn't work in older browsers (eg IE8 I think). Polyfill here.
注意:日期。现在()在旧浏览器中不起作用(如IE8)。Polyfill这里。
UPDATE June 2015
@socketpair pointed out my sloppiness. As s/he says "Some day in year have 23 hours, and some 25 due to timezone rules".
@socketpair指出了我的马虎。正如s/他所说,“一年中的某一天有23个小时,而有25个小时是由于时区规则”。
To expand on that, the answer above will have daylightsaving inaccuracies in the case where you want to calculate the LOCAL day 5 days ago in a timezone with daylightsaving changes and you
若要扩展这一点,上面的答案将会在您想要计算的情况下,在您想要计算的地方,在5天前的一个时区里,您需要使用daylightsaving changes和您。
- assume (wrongly) that
Date.now()
gives you the current LOCAL now time, or - 假设(错误地)那个日期。现在()给您当前本地时间,或。
- use
.toString()
which returns the local date and therefore is incompatible with theDate.now()
base date in UTC. - 使用. tostring()返回本地日期,因此与日期不兼容。
However, it works if you're doing your math all in UTC, eg
然而,如果你在UTC(例如)做你的数学,它就会起作用。
A. You want the UTC date 5 days ago from NOW (UTC)
A.您需要5天前的UTC时间(UTC)
var newDate = Date.now() + -5*24*3600*1000; // date 5 days ago in milliseconds UTC
new Date(newDate).toUTCString(); // or .toISOString(), BUT NOT toString
B. You start with a UTC base date other than "now", using Date.UTC()
B.你从一个UTC基本日期开始,而不是“现在”,使用日期。UTC()
newDate = new Date(Date.UTC(2015, 3, 1)).getTime() + -5*24*3600000;
new Date(newDate).toUTCString(); // or .toISOString BUT NOT toString
#9
7
A few of the existing solutions were close, but not quite exactly what I wanted. This function works with both positive or negative values and handles boundary cases.
现有的一些解决方案很接近,但并不完全符合我的要求。这个函数与正负值一起工作,处理边界情况。
function addDays(date, days) {
return new Date(
date.getFullYear(),
date.getMonth(),
date.getDate() + days,
date.getHours(),
date.getMinutes(),
date.getSeconds(),
date.getMilliseconds()
);
}
#10
4
I find a problem with the getDate()/setDate() method is that it too easily turns everything into milliseconds, and the syntax is sometimes hard for me to follow.
我发现getDate()/setDate()方法的一个问题是,它太容易将所有东西转换成毫秒,而语法有时对我来说很难遵循。
Instead I like to work off the fact that 1 day = 86,400,000 milliseconds.
相反,我喜欢计算1天= 86,400,000毫秒的事实。
So, for your particular question:
所以,对于你的特殊问题:
today = new Date()
days = 86400000 //number of milliseconds in a day
fiveDaysAgo = new Date(today - (5*days))
Works like a charm.
就像一个魅力。
I use this method all the time for doing rolling 30/60/365 day calculations.
我一直用这个方法来做30/60/365天计算。
You can easily extrapolate this to create units of time for months, years, etc.
你可以很容易地推断出它能创造几个月、几年等等的时间单位。
#11
3
function addDays (date, daysToAdd) {
var _24HoursInMilliseconds = 86400000;
return new Date(date.getTime() + daysToAdd * _24HoursInMilliseconds);
};
var now = new Date();
var yesterday = addDays(now, - 1);
var tomorrow = addDays(now, 1);
#12
1
The top answers led to a bug in my code where on the first of the month it would set a future date in the current month. Here is what I did,
上面的答案导致了我的代码中出现了一个bug,在本月的第一个月,它将在本月设置一个未来的日期。这就是我所做的,
curDate = new Date(); // Took current date as an example
prvDate = new Date(0); // Date set to epoch 0
prvDate.setUTCMilliseconds((curDate - (5 * 24 * 60 * 60 * 1000))); //Set epoch time
#13
1
A easy way to manage dates is use Moment.js
管理日期的一种简单方法是使用Moment.js。
You can use add
. Example
您可以使用add. Example。
var startdate = "20.03.2014";
var new_date = moment(startdate, "DD.MM.YYYY");
new_date.add(5, 'days'); //Add 5 days to start date
alert(new_date);
Docs http://momentjs.com/docs/#/manipulating/add/
文档http://momentjs.com/docs/ /操作/添加/
#14
1
for me all the combinations worked fine with below code snipplet , the snippet is for Angular-2 implementation , if you need to add days , pass positive numberofDays , if you need to substract pass negative numberofDays
对于我来说,所有的组合都可以很好地使用下面的代码snipplet,这段代码是用于Angular-2实现的,如果您需要添加天数,那么要传递正数的天数,如果您需要减去通过负数的天数。
function addSubstractDays(date: Date, numberofDays: number): Date {
let d = new Date(date);
return new Date(
d.getFullYear(),
d.getMonth(),
(d.getDate() + numberofDays)
);
}
#15
1
I get good mileage out of date.js:
我能从约会中得到好处。
http://www.datejs.com/
d = new Date();
d.add(-10).days(); // subtract 10 days
Nice!
好了!
Website includes this beauty:
网站包括这种美:
Datejs doesn’t just parse strings, it slices them cleanly in two
Datejs不只是解析字符串,它将它们整齐地分成两部分。
#16
0
When setting the date, the date converts to milliseconds, so you need to convert it back to a date:
设置日期时,日期转换为毫秒,因此您需要将其转换为日期:
This method also take into consideration, new year change etc.
这种方法也要考虑到,新年变化等。
function addDays( date, days ) {
var dateInMs = date.setDate(date.getDate() - days);
return new Date(dateInMs);
}
var date_from = new Date();
var date_to = addDays( new Date(), parseInt(days) );
#17
0
You can using Javascript.
您可以使用Javascript。
var CurrDate = new Date(); // Current Date
var numberOfDays = 5;
var days = CurrDate.setDate(CurrDate.getDate() + numberOfDays);
alert(days); // It will print 5 days before today
For PHP,
对于PHP,
$date = date('Y-m-d', strtotime("-5 days")); // it shows 5 days before today.
echo $date;
Hope it will help you.
希望它能帮到你。
#18
0
var d = new Date();
document.write('Today is: ' + d.toLocaleString());
d.setDate(d.getDate() - 31);
document.write('<br>5 days ago was: ' + d.toLocaleString());
#19
0
I like the following because it is one line. Not perfect with DST changes but usually good enough for my needs.
我喜欢下面这一行,因为它是一行。虽然不完美,但通常足够满足我的需要。
var fiveDaysAgo = new Date(new Date() - (1000*60*60*24*5));
#20
0
var d = new Date();
document.write('Today is: ' + d.toLocaleString());
d.setDate(d.getDate() - 100);
document.write('<br>100 days ago was: ' + d.toLocaleString());
#21
0
without using the second variable, you can replace 7 for with your back x days
不使用第二个变量,可以用后x天替换7。
let d=new Date(new Date().getTime() - (7 * 24 * 60 * 60 * 1000))
让d=new Date(new Date().getTime() - (7 * 24 * 60 * 60 * 1000))
#22
0
I converted into millisecond and deducted days else month and year won't change and logical
我转换成毫秒,扣除天数,其他月份和年份不会变化和逻辑。
var numberOfDays = 10;//number of days need to deducted or added
var date = "01-01-2018"// date need to change
var dt = new Date(parseInt(date.substring(6), 10), // Year
parseInt(date.substring(3,5), 10) - 1, // Month (0-11)
parseInt(date.substring(0,2), 10));
var new_dt = dt.setMilliseconds(dt.getMilliseconds() - numberOfDays*24*60*60*1000);
new_dt = new Date(new_dt);
var changed_date = new_dt.getDate()+"-"+(new_dt.getMonth()+1)+"-"+new_dt.getFullYear();
Hope helps
希望能帮助
#23
0
If you want to both subtract a number of days and format your date in a human readable format, you should consider creating a custom DateHelper
object that looks something like this :
如果您想要同时减去若干天并以人类可读的格式格式化日期,那么您应该考虑创建一个自定义的DateHelper对象,它看起来如下:
var DateHelper = {
addDays : function(aDate, numberOfDays) {
aDate.setDate(aDate.getDate() + numberOfDays); // Add numberOfDays
return aDate; // Return the date
},
format : function format(date) {
return [
("0" + date.getDate()).slice(-2), // Get day and pad it with zeroes
("0" + (date.getMonth()+1)).slice(-2), // Get month and pad it with zeroes
date.getFullYear() // Get full year
].join('/'); // Glue the pieces together
}
}
// With this helper, you can now just use one line of readable code to :
// ---------------------------------------------------------------------
// 1. Get the current date
// 2. Subtract 5 days
// 3. Format it
// 4. Output it
// ---------------------------------------------------------------------
document.body.innerHTML = DateHelper.format(DateHelper.addDays(new Date(), -5));
(see also this Fiddle)
(参见本小提琴)
#24
-1
var daysToSubtract = 3;
$.datepicker.formatDate('yy/mm/dd', new Date() - daysToSubtract) ;
#25
-2
var my date = new Date().toISOString().substring(0, 10);
it can give you only date like 2014-06-20. hope will help
它只能给你像2014-06-20那样的日期。希望能帮助
#1
587
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());
#2
50
var dateOffset = (24*60*60*1000) * 5; //5 days
var myDate = new Date();
myDate.setTime(myDate.getTime() - dateOffset);
If you're performing lots of headachy date manipulation throughout your web application, DateJS will make your life much easier:
如果您在整个web应用程序中执行大量的headachy日期操作,DateJS将使您的生活更加轻松:
http://simonwillison.net/2007/Dec/3/datejs/
http://simonwillison.net/2007/Dec/3/datejs/
#3
44
It goes something like this:
它是这样的:
var d = new Date(); // today!
var x = 5; // go back 5 days!
d.setDate(d.getDate() - x);
#4
18
I noticed that the getDays+ X doesn't work over day/month boundaries. Using getTime works as long as your date is not before 1970.
我注意到getDays+ X不能超过一天/月的边界。使用getTime工作只要你的日期不在1970年之前。
var todayDate = new Date(), weekDate = new Date();
weekDate.setTime(todayDate.getTime()-(7*24*3600000));
#5
11
I made this prototype for Date so that I could pass negative values to subtract days and positive values to add days.
我做了这个原型,以便我可以通过负值来减去天数和正值来增加天数。
if(!Date.prototype.adjustDate){
Date.prototype.adjustDate = function(days){
var date;
days = days || 0;
if(days === 0){
date = new Date( this.getTime() );
} else if(days > 0) {
date = new Date( this.getTime() );
date.setDate(date.getDate() + days);
} else {
date = new Date(
this.getFullYear(),
this.getMonth(),
this.getDate() - Math.abs(days),
this.getHours(),
this.getMinutes(),
this.getSeconds(),
this.getMilliseconds()
);
}
this.setTime(date.getTime());
return this;
};
}
So, to use it i can simply write:
因此,我可以简单地写:
var date_subtract = new Date().adjustDate(-4),
date_add = new Date().adjustDate(4);
#6
9
split your date into parts, then return a new Date with the adjusted values
将你的约会分成不同的部分,然后用调整后的值返回一个新的日期。
function DateAdd(date, type, amount){
var y = date.getFullYear(),
m = date.getMonth(),
d = date.getDate();
if(type === 'y'){
y += amount;
};
if(type === 'm'){
m += amount;
};
if(type === 'd'){
d += amount;
};
return new Date(y, m, d);
}
Remember that the months are zero based, but the days are not. ie new Date(2009, 1, 1) == 01 February 2009, new Date(2009, 1, 0) == 31 January 2009;
记住,每个月都是零,但日子不是。2009年2月1日,2009年2月1日,2009年2月1日,2009年1月31日;
#7
9
get moment.js. All the cool kids use it. It has more formatting options, etc. Where
得到moment.js。所有的酷孩子都用它。它有更多的格式化选项,等等。
var n = 5;
var dateMnsFive = moment(<your date>).subtract(n , 'day');
Optional! Convert to JS Date obj for Angular binding.
可选!转换到JS日期obj的角度绑定。
var date = new Date(dateMnsFive.toISOString());
Optional! Format
可选!格式
var date = dateMnsFive.format("YYYY-MM-DD");
#8
8
I like doing the maths in milliseconds. So use Date.now()
我喜欢用毫秒计算。所以使用Date.now()
var newDate = Date.now() + -5*24*3600*1000; // date 5 days ago in milliseconds
and if you like it formatted
如果你喜欢它的格式。
new Date(newDate).toString(); // or .toUTCString or .toISOString ...
NOTE: Date.now()
doesn't work in older browsers (eg IE8 I think). Polyfill here.
注意:日期。现在()在旧浏览器中不起作用(如IE8)。Polyfill这里。
UPDATE June 2015
@socketpair pointed out my sloppiness. As s/he says "Some day in year have 23 hours, and some 25 due to timezone rules".
@socketpair指出了我的马虎。正如s/他所说,“一年中的某一天有23个小时,而有25个小时是由于时区规则”。
To expand on that, the answer above will have daylightsaving inaccuracies in the case where you want to calculate the LOCAL day 5 days ago in a timezone with daylightsaving changes and you
若要扩展这一点,上面的答案将会在您想要计算的情况下,在您想要计算的地方,在5天前的一个时区里,您需要使用daylightsaving changes和您。
- assume (wrongly) that
Date.now()
gives you the current LOCAL now time, or - 假设(错误地)那个日期。现在()给您当前本地时间,或。
- use
.toString()
which returns the local date and therefore is incompatible with theDate.now()
base date in UTC. - 使用. tostring()返回本地日期,因此与日期不兼容。
However, it works if you're doing your math all in UTC, eg
然而,如果你在UTC(例如)做你的数学,它就会起作用。
A. You want the UTC date 5 days ago from NOW (UTC)
A.您需要5天前的UTC时间(UTC)
var newDate = Date.now() + -5*24*3600*1000; // date 5 days ago in milliseconds UTC
new Date(newDate).toUTCString(); // or .toISOString(), BUT NOT toString
B. You start with a UTC base date other than "now", using Date.UTC()
B.你从一个UTC基本日期开始,而不是“现在”,使用日期。UTC()
newDate = new Date(Date.UTC(2015, 3, 1)).getTime() + -5*24*3600000;
new Date(newDate).toUTCString(); // or .toISOString BUT NOT toString
#9
7
A few of the existing solutions were close, but not quite exactly what I wanted. This function works with both positive or negative values and handles boundary cases.
现有的一些解决方案很接近,但并不完全符合我的要求。这个函数与正负值一起工作,处理边界情况。
function addDays(date, days) {
return new Date(
date.getFullYear(),
date.getMonth(),
date.getDate() + days,
date.getHours(),
date.getMinutes(),
date.getSeconds(),
date.getMilliseconds()
);
}
#10
4
I find a problem with the getDate()/setDate() method is that it too easily turns everything into milliseconds, and the syntax is sometimes hard for me to follow.
我发现getDate()/setDate()方法的一个问题是,它太容易将所有东西转换成毫秒,而语法有时对我来说很难遵循。
Instead I like to work off the fact that 1 day = 86,400,000 milliseconds.
相反,我喜欢计算1天= 86,400,000毫秒的事实。
So, for your particular question:
所以,对于你的特殊问题:
today = new Date()
days = 86400000 //number of milliseconds in a day
fiveDaysAgo = new Date(today - (5*days))
Works like a charm.
就像一个魅力。
I use this method all the time for doing rolling 30/60/365 day calculations.
我一直用这个方法来做30/60/365天计算。
You can easily extrapolate this to create units of time for months, years, etc.
你可以很容易地推断出它能创造几个月、几年等等的时间单位。
#11
3
function addDays (date, daysToAdd) {
var _24HoursInMilliseconds = 86400000;
return new Date(date.getTime() + daysToAdd * _24HoursInMilliseconds);
};
var now = new Date();
var yesterday = addDays(now, - 1);
var tomorrow = addDays(now, 1);
#12
1
The top answers led to a bug in my code where on the first of the month it would set a future date in the current month. Here is what I did,
上面的答案导致了我的代码中出现了一个bug,在本月的第一个月,它将在本月设置一个未来的日期。这就是我所做的,
curDate = new Date(); // Took current date as an example
prvDate = new Date(0); // Date set to epoch 0
prvDate.setUTCMilliseconds((curDate - (5 * 24 * 60 * 60 * 1000))); //Set epoch time
#13
1
A easy way to manage dates is use Moment.js
管理日期的一种简单方法是使用Moment.js。
You can use add
. Example
您可以使用add. Example。
var startdate = "20.03.2014";
var new_date = moment(startdate, "DD.MM.YYYY");
new_date.add(5, 'days'); //Add 5 days to start date
alert(new_date);
Docs http://momentjs.com/docs/#/manipulating/add/
文档http://momentjs.com/docs/ /操作/添加/
#14
1
for me all the combinations worked fine with below code snipplet , the snippet is for Angular-2 implementation , if you need to add days , pass positive numberofDays , if you need to substract pass negative numberofDays
对于我来说,所有的组合都可以很好地使用下面的代码snipplet,这段代码是用于Angular-2实现的,如果您需要添加天数,那么要传递正数的天数,如果您需要减去通过负数的天数。
function addSubstractDays(date: Date, numberofDays: number): Date {
let d = new Date(date);
return new Date(
d.getFullYear(),
d.getMonth(),
(d.getDate() + numberofDays)
);
}
#15
1
I get good mileage out of date.js:
我能从约会中得到好处。
http://www.datejs.com/
d = new Date();
d.add(-10).days(); // subtract 10 days
Nice!
好了!
Website includes this beauty:
网站包括这种美:
Datejs doesn’t just parse strings, it slices them cleanly in two
Datejs不只是解析字符串,它将它们整齐地分成两部分。
#16
0
When setting the date, the date converts to milliseconds, so you need to convert it back to a date:
设置日期时,日期转换为毫秒,因此您需要将其转换为日期:
This method also take into consideration, new year change etc.
这种方法也要考虑到,新年变化等。
function addDays( date, days ) {
var dateInMs = date.setDate(date.getDate() - days);
return new Date(dateInMs);
}
var date_from = new Date();
var date_to = addDays( new Date(), parseInt(days) );
#17
0
You can using Javascript.
您可以使用Javascript。
var CurrDate = new Date(); // Current Date
var numberOfDays = 5;
var days = CurrDate.setDate(CurrDate.getDate() + numberOfDays);
alert(days); // It will print 5 days before today
For PHP,
对于PHP,
$date = date('Y-m-d', strtotime("-5 days")); // it shows 5 days before today.
echo $date;
Hope it will help you.
希望它能帮到你。
#18
0
var d = new Date();
document.write('Today is: ' + d.toLocaleString());
d.setDate(d.getDate() - 31);
document.write('<br>5 days ago was: ' + d.toLocaleString());
#19
0
I like the following because it is one line. Not perfect with DST changes but usually good enough for my needs.
我喜欢下面这一行,因为它是一行。虽然不完美,但通常足够满足我的需要。
var fiveDaysAgo = new Date(new Date() - (1000*60*60*24*5));
#20
0
var d = new Date();
document.write('Today is: ' + d.toLocaleString());
d.setDate(d.getDate() - 100);
document.write('<br>100 days ago was: ' + d.toLocaleString());
#21
0
without using the second variable, you can replace 7 for with your back x days
不使用第二个变量,可以用后x天替换7。
let d=new Date(new Date().getTime() - (7 * 24 * 60 * 60 * 1000))
让d=new Date(new Date().getTime() - (7 * 24 * 60 * 60 * 1000))
#22
0
I converted into millisecond and deducted days else month and year won't change and logical
我转换成毫秒,扣除天数,其他月份和年份不会变化和逻辑。
var numberOfDays = 10;//number of days need to deducted or added
var date = "01-01-2018"// date need to change
var dt = new Date(parseInt(date.substring(6), 10), // Year
parseInt(date.substring(3,5), 10) - 1, // Month (0-11)
parseInt(date.substring(0,2), 10));
var new_dt = dt.setMilliseconds(dt.getMilliseconds() - numberOfDays*24*60*60*1000);
new_dt = new Date(new_dt);
var changed_date = new_dt.getDate()+"-"+(new_dt.getMonth()+1)+"-"+new_dt.getFullYear();
Hope helps
希望能帮助
#23
0
If you want to both subtract a number of days and format your date in a human readable format, you should consider creating a custom DateHelper
object that looks something like this :
如果您想要同时减去若干天并以人类可读的格式格式化日期,那么您应该考虑创建一个自定义的DateHelper对象,它看起来如下:
var DateHelper = {
addDays : function(aDate, numberOfDays) {
aDate.setDate(aDate.getDate() + numberOfDays); // Add numberOfDays
return aDate; // Return the date
},
format : function format(date) {
return [
("0" + date.getDate()).slice(-2), // Get day and pad it with zeroes
("0" + (date.getMonth()+1)).slice(-2), // Get month and pad it with zeroes
date.getFullYear() // Get full year
].join('/'); // Glue the pieces together
}
}
// With this helper, you can now just use one line of readable code to :
// ---------------------------------------------------------------------
// 1. Get the current date
// 2. Subtract 5 days
// 3. Format it
// 4. Output it
// ---------------------------------------------------------------------
document.body.innerHTML = DateHelper.format(DateHelper.addDays(new Date(), -5));
(see also this Fiddle)
(参见本小提琴)
#24
-1
var daysToSubtract = 3;
$.datepicker.formatDate('yy/mm/dd', new Date() - daysToSubtract) ;
#25
-2
var my date = new Date().toISOString().substring(0, 10);
it can give you only date like 2014-06-20. hope will help
它只能给你像2014-06-20那样的日期。希望能帮助