gpt4 book ai didi

JavaScript 日期,提前 30 天获取

转载 作者:行者123 更新时间:2023-12-03 04:41:49 25 4
gpt4 key购买 nike

我正在尝试设置 JSON 对象的结束日期。结束日期等于开始日期后 30 天。有时这会返回正确的日期,有时则不会。

这是GetDateSchedulerFormatted函数

GetDateSchedulerFormatted(date) {
function pad(s) { return (s < 10) ? '0' + s : s; }
var d = new Date(date);
// yyy-MM-dd
return [pad(d.getDate()), pad(d.getMonth() + 1), d.getFullYear()].join('/') + " " + pad(d.getHours()) + ":" + pad(d.getMinutes());
}

在此示例中,代码返回正确的日期

//activity.startDate = 2017-07-02T00:00:00-08:00
var startDate = this.GetDateSchedulerFormatted(activity.startDate); //start date 07/02/2017 00:00 d/m/yyy

var newDate = new Date(startDate); // returns Tue Aug 01 2017 00:00:00 GMT-0700 (Pacific Daylight Time) also 1 day off

var endDate = this.GetDateSchedulerFormatted(new Date(newDate.setTime(newDate.getTime() + 30 * 86400000))); //returns correct date 01/08/2017 00:00 d/m/yyyy

在下一个示例中,它将返回一年后的日期

    //activity.startDate = 2016-12-12T00:00:00-08:00
var startDate = this.GetDateSchedulerFormatted(activity.startDate); //returns 12/12/2016 00:00 d/m/yyy

var newDate = new Date(startDate); // returns Wed Jan 11 2017 00:00:00 GMT-0800 1 month ahead

var endDate = this.GetDateSchedulerFormatted(new Date(newDate.setTime(newDate.getTime() + 30 * 86400000))); //returns 11/01/2017 00:0 d/m/yyy

在此示例中返回相同的确切日期

        //activity.startDate = 2017-02-01T00:00:00-08:00
var startDate = this.GetDateSchedulerFormatted(activity.startDate); //returns 01/02/2017 00:00 d/m/yyy

var newDate = new Date(startDate); // returns Wed Feb 01 2017 00:00:00 GMT-0800

var endDate = this.GetDateSchedulerFormatted(new Date(newDate.setTime(newDate.getTime() + 30 * 86400000))); //returns 01/02/2017 00:00 the same date, it's not 30 days ahead

然后在最后一个例子中我得到 NaN/NaN/NaN NaN:NaN

        //activity.startDate = 2017-02-25T00:00:00-08:00
var startDate = this.GetDateSchedulerFormatted(activity.startDate); //returns 25/02/2017 00:00 d/m/yyy

var newDate = new Date(startDate); // returns invalid date

var endDate = this.GetDateSchedulerFormatted(new Date(newDate.setTime(newDate.getTime() + 30 * 86400000))); //returns NaN/NaN/NaN NaN:NaN

我也尝试过new Date(Date.parse(startDate));

最佳答案

你真的不需要图书馆。为日期添加一个月相当简单,从以下开始:

date.setMonth(date.getMonth() + 1);

这会保留与日期相关的时间,甚至超出夏令时范围,但可能会将日期推到下个月月底之后,例如1 月 31 日加 1 个月得到 2 月 31 日,即 3 月 2 日或 3 月 3 日(取决于是否是闰年)。

因此需要进行检查,如果日期不相同,则它会滚动一个月,因此将其设置为上个月的最后一天。编写为添加任意数量月份的函数:

function addMonths(date, months) {
var d = date.getDate();
date.setMonth(date.getMonth() + +months);
if (date.getDate() != d) {
date.setDate(0);
}
return date;
}

// Add 12 months to 29 Feb, 2016
var d = new Date(2016,1,29)
console.log(addMonths(d, 12).toString()); // 28 Feb 2017

添加更加容易,请参阅 Add +1 to current date它很容易适应添加任意天数(这意味着这个问题实际上是重复的)。

那么,回到你的代码。

Here is the GetDateSchedulerFormatted function

function GetDateSchedulerFormatted(date) {
function pad(s) {
return (s < 10) ? '0' + s : s;
}
var d = new Date(date);
// yyy-MM-dd
return [pad(d.getDate()),
pad(d.getMonth() + 1),
d.getFullYear()
].join('/') + " " +
pad(d.getHours()) + ":" +
pad(d.getMinutes());
}

// In this example the code returns the correct date
var activity = {};
activity.startDate = '2017-07-02T00:00:00-08:00';
var startDate = GetDateSchedulerFormatted(activity.startDate); //start date 07/02/2017 00:00 d/m/yyy

var newDate = new Date(startDate); // returns Tue Aug 01 2017 00:00:00 GMT-0700 (Pacific Daylight Time) also 1 day off

var endDate = GetDateSchedulerFormatted(new Date(newDate.setTime(newDate.getTime() + 30 * 86400000))); //returns

correct date 01/08/2017

您的问题是,您以有效的 ISO 8601 格式字符串“2017-07-02T00:00:00-08:00”开始,但随后将其重新格式化为您自己时区的格式(例如“02/07/2017 00:00'(如果您的时区是 -0800),然后使用 Date 构造函数解析它,这是一个非常糟糕的主意。它可能被视为 2 月 7 日,所以我不知道你怎么能说它返回你从 7 月 2 日开始时的正确日期。添加 1 个月应该是 8 月 2 日,而不是 8 月 1 日(尽管您确实添加了 30 天而不是 1 个月)。最后,如果您跨越夏令时边界,您可能会损失或增加一个小时,因此日期可能是前一天的 23:00 或从 00:00 到 01:00。

请注意,您有:

07/02/2017 00:00 d/m/yyy
^^^^^^^

这不是 7 月 2 日,而是 2 月 7 日。

您的其余问题类似。

无论如何,如果您对使用库感到满意,那很好。只是想我会指出你哪里出错了。

这是您的代码,经过调整后可以在此处运行,并显示错误:

function GetDateSchedulerFormatted(date) {
function pad(s) {
return (s < 10) ? '0' + s : s;
}
var d = new Date(date);
// yyy-MM-dd
return [pad(d.getDate()),
pad(d.getMonth() + 1),
d.getFullYear()
].join('/') + " " +
pad(d.getHours()) + ":" +
pad(d.getMinutes());
}

// In this example the code returns the correct date
var activity = {};
activity.startDate = '2017-07-02T00:00:00-08:00';
var startDate = GetDateSchedulerFormatted(activity.startDate); //start date 07/02/2017 00:00 d/m/yyy

var newDate = new Date(startDate); // returns Tue Aug 01 2017 00:00:00 GMT-0700 (Pacific Daylight Time) also 1 day off

var endDate = GetDateSchedulerFormatted(new Date(newDate.setTime(newDate.getTime() + 30 * 86400000))); //returns correct date 01/08/2017 00: 00 d / m / yyyy

console.log('activity.startDate: ' + activity.startDate +
'\nstartDate : ' + startDate +
'\nnewDate : ' + GetDateSchedulerFormatted(newDate) +
'\nendDate : ' + endDate);

// In this next example it returns the date 1 year off

activity.startDate = '2016-12-12T00:00:00-08:00';
var startDate = GetDateSchedulerFormatted(activity.startDate); //returns 12/12/2016 00:00 d/m/yyy

var newDate = new Date(startDate); // returns Wed Jan 11 2017 00:00:00 GMT-0800 1 month ahead

var endDate = GetDateSchedulerFormatted(new Date(newDate.setTime(newDate.getTime() + 30 * 86400000))); //returns 11/01/2017 00:0 d/m/yyy
console.log('activity.startDate: ' + activity.startDate +
'\nstartDate : ' + startDate +
'\nnewDate : ' + GetDateSchedulerFormatted(newDate) +
'\nendDate : ' + endDate);


// In this example the same exact date is returned


activity.startDate = '2017-02-01T00:00:00-08:00';
var startDate = GetDateSchedulerFormatted(activity.startDate); //returns 01/02/2017 00:00 d/m/yyy

var newDate = new Date(startDate); // returns Wed Feb 01 2017 00:00:00 GMT-0800

var endDate = GetDateSchedulerFormatted(new Date(newDate.setTime(newDate.getTime() + 30 * 86400000))); //returns 01/02/2017 00:00 the same date, it 's not 30 days ahead
console.log('activity.startDate: ' + activity.startDate +
'\nstartDate : ' + startDate +
'\nnewDate : ' + GetDateSchedulerFormatted(newDate) +
'\nendDate : ' + endDate);

// Then in this final example I get NaN / NaN / NaN NaN: NaN

activity.startDate = '2017-02-25T00:00:00-08:00';
var startDate = GetDateSchedulerFormatted(activity.startDate); //returns 25/02/2017 00:00 d/m/yyy

var newDate = new Date(startDate); // returns invalid date

var endDate = GetDateSchedulerFormatted(new Date(newDate.setTime(newDate.getTime() + 30 * 86400000))); //returns NaN/NaN/NaN NaN:NaN
console.log('activity.startDate: ' + activity.startDate +
'\nstartDate : ' + startDate +
'\nnewDate : ' + GetDateSchedulerFormatted(newDate) +
'\nendDate : ' + endDate);

关于JavaScript 日期,提前 30 天获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43052002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com