Javascript 在应用程序脚本中将天添加到历史日期
我试图提取一些历史数据,这些数据有各种各样的时间戳。我想使用一个用户选择的日期,然后拉所有天+一些额外的天。 日期1是Javascript 在应用程序脚本中将天添加到历史日期,javascript,date,datetime,math,Javascript,Date,Datetime,Math,我试图提取一些历史数据,这些数据有各种各样的时间戳。我想使用一个用户选择的日期,然后拉所有天+一些额外的天。 日期1是 这不管用。我似乎不知道如何将“10天”添加到Date1变量。您需要Date对象的getDate函数,并且在实例化新的Date时需要传递正确的值 试试这个: var Date1 = "Thu Oct 22 00:00:00 GMT-04:00 2020" var startDate = new Date(Date1); // add it to startDa
这不管用。我似乎不知道如何将“10天”添加到Date1变量。您需要
Date
对象的getDate
函数,并且在实例化新的Date
时需要传递正确的值
试试这个:
var Date1 = "Thu Oct 22 00:00:00 GMT-04:00 2020"
var startDate = new Date(Date1);
// add it to startDate, using getDate()
startDate.setDate(startDate.getDate() + 10);
// now startDate will be 10 days later:
startDate; // Sun Nov 01 00:00:00 GMT-04:00 2020
// if you want an entirely new Date object, then instantiate a new one:
var Enddate = new Date(startDate);
如果您想要两个不同的变量,那么您可以使用与您尝试的方法类似的方法,如下所示:
var Date1 = "Thu Oct 22 00:00:00 GMT-04:00 2020"
var startDate = new Date(Date1), second = new Date(Date1); // we're using another temporary variable
// add the days
second.setDate(second.getDate() + 10);
// now use Enddate with this
var Enddate = new Date(second);
Enddate; // Sun Nov 01 00:00:00 GMT-04:00 2020
您需要
Date
对象的getDate
函数,并且在实例化新的Date
时需要传递正确的值
试试这个:
var Date1 = "Thu Oct 22 00:00:00 GMT-04:00 2020"
var startDate = new Date(Date1);
// add it to startDate, using getDate()
startDate.setDate(startDate.getDate() + 10);
// now startDate will be 10 days later:
startDate; // Sun Nov 01 00:00:00 GMT-04:00 2020
// if you want an entirely new Date object, then instantiate a new one:
var Enddate = new Date(startDate);
如果您想要两个不同的变量,那么您可以使用与您尝试的方法类似的方法,如下所示:
var Date1 = "Thu Oct 22 00:00:00 GMT-04:00 2020"
var startDate = new Date(Date1), second = new Date(Date1); // we're using another temporary variable
// add the days
second.setDate(second.getDate() + 10);
// now use Enddate with this
var Enddate = new Date(second);
Enddate; // Sun Nov 01 00:00:00 GMT-04:00 2020
您不能只向
日期
对象添加一个数字来获取新日期
您可以通过以下方式添加到日期:
getDate()
setDate()设置新日期
const Date1=“10月22日星期四00:00:00 GMT-04:00 2020”;
const addDays=10;
const startDate=新日期(日期1);
const startDays=startDate.getDate();
const newDate=新日期(startDate);
newDate.setDate(startDays+addDays);
log('startDate:',startDate.toISOString());
log('newDate:',newDate.toISOString())
您不能只向日期
对象添加一个数字来获取新日期
您可以通过以下方式添加到日期:
getDate()
setDate()设置新日期
const Date1=“10月22日星期四00:00:00 GMT-04:00 2020”;
const addDays=10;
const startDate=新日期(日期1);
const startDays=startDate.getDate();
const newDate=新日期(startDate);
newDate.setDate(startDays+addDays);
log('startDate:',startDate.toISOString());
log('newDate:',newDate.toISOString())代码>“这不起作用。”这是什么意思?你有错误吗?日期和你想象的不一样吗?我得到的Enddate日志是:Wed Dec 31 19:00:00 GMT-05:00 1969,这是不正确的。“这不起作用。”这是什么意思?你有错误吗?日期和你想象的不一样吗?我得到的Enddate日志是:Wed Dec 31 19:00:00 GMT-05:00 1969,这是不正确的。太棒了!这完全符合需要。没有意识到我需要在变量之后使用getDate()!这完全符合需要。我没有意识到我需要在变量之后使用getDate(),通过增加24小时来增加一天不是一个好主意,因为在遵守夏时制的地方,并不是所有的日子都是24小时长的。请参阅副本。通过添加24小时来添加一天不是一个好主意,因为在使用夏时制的地方,并非所有的日子都是24小时长的。请看副本。