gpt4 book ai didi

JavaScript:如何获取 7 天前、上个月、本周的日期

转载 作者:行者123 更新时间:2023-11-29 17:57:44 25 4
gpt4 key购买 nike

我正在构建一个函数,我将根据用户作为参数发送给它的内容动态生成日期。我需要为上周、本周、上个月、本月、过去 7 天、过去 30 天等案例创建日期。由于我要将日期从日期发送到数据库,因此我需要将它们格式化为 yyyy-mm-dd。我的问题是如何获得上周结束和上周开始、本周开始的日期,以及当我从当前日期减去 7 或 30 时的正确日期。

到目前为止,这是我的代码,但它只适用于当月的日期:

function myDate(day){
var date = new Date();
var dd = date.getDate() - day;
var mm = date.getMonth()+1; //January is 0!
var yyyy = date.getFullYear();
if(dd<10){
dd='0'+dd
}
if(mm<10){
mm='0'+mm
}
var newDate = yyyy+'-'+mm+'-'+dd;

return newDate;
}

var today = myDate(0);
var yesterday = myDate(1);
var sevenDaysAgo = myDate(7);
var thirtyDaysAgo = myDate(30);

更新后的代码:

如果有人需要的话,这就是我使用 moment.js 的方式:

var date = moment().format("YYYY-MM-DD");
var yesterday = moment().subtract(1, 'day').format('YYYY-MM-DD');
var sevenDaysAgo = moment().subtract(7, 'day').format('YYYY-MM-DD');
var thirtyDaysAgo = moment().subtract(30, 'day').format('YYYY-MM-DD');
var lastWeekStart = moment(date).weekday(-6).format('YYYY-MM-DD');
var lastWeekEnd = moment(date).weekday(1).format('YYYY-MM-DD');
var thisWeekStart = moment(date).weekday(1).format('YYYY-MM-DD');
var startOfMonth = moment().startOf('month').format('YYYY-MM-DD');
var startOfLastMonth = moment().subtract(1, 'month').startOf('month').format('YYYY-MM-DD');
var endOfLastMonth = moment().subtract(1, 'month').endOf('month').format('YYYY-MM-DD');

最佳答案

DIY 日期操作

对于好奇或厌恶库的人,这里有一些示例展示了如何在普通 JavaScript 中创建自己的函数来为您执行这些计算:

/* Define new prototype methods on Date object. */
// Returns Date as a String in YYYY-MM-DD format.
Date.prototype.toISODateString = function () {
return this.toISOString().substr(0,10);
};

// Returns new Date object offset `n` days from current Date object.
Date.prototype.toDateFromDays = function (n) {
n = parseInt(n) || 0;
var newDate = new Date(this.getTime());
newDate.setDate(this.getDate() + n);
return newDate;
};

// Returns new Date object from start of week of current Date object
// optionally offset `n` weeks from week of current Date object.
Date.prototype.toStartOfWeek = function (n) {
var newDate = new Date(this.getTime());
newDate.setDate(this.getDate() - this.getDay());
return n ? newDate.toDateFromDays(n * 7) : newDate;
};

// Returns new Date object from start of month of current Date object
// optionally offset `n` months from month of current Date object.
Date.prototype.toStartOfMonth = function (n) {
n = parseInt(n) || 0;
var newDate = new Date(this.getTime());
newDate.setMonth(this.getMonth() + n, 1);
return newDate;
};

/* Instantiate a Date. */
var today = new Date();

/* Chain all the things. */
console.log(
'Today: ', today.toISODateString()
);
console.log(
'7 days ago: ', today.toDateFromDays(-7)
.toISODateString()
);
console.log(
'30 days ago: ', today.toDateFromDays(-30)
.toISODateString()
);
console.log(
'90 days from now: ', today.toDateFromDays(90)
.toISODateString()
);
console.log(
'Start of this week: ', today.toStartOfWeek()
.toISODateString()
);
console.log(
'End of this week: ', today.toStartOfWeek(1)
.toDateFromDays(-1)
.toISODateString()
);
console.log(
'Start of last week: ', today.toStartOfWeek(-1)
.toISODateString()
);
console.log(
'End of last week: ', today.toStartOfWeek()
.toDateFromDays(-1)
.toISODateString()
);
console.log(
'Start of next week: ', today.toStartOfWeek(1)
.toISODateString()
);
console.log(
'End of next week: ', today.toStartOfWeek(2)
.toDateFromDays(-1)
.toISODateString()
);
console.log(
'Start of this month: ', today.toStartOfMonth()
.toISODateString()
);
console.log(
'End of this month: ', today.toStartOfMonth(1)
.toDateFromDays(-1)
.toISODateString()
);
console.log(
'Start of last month: ', today.toStartOfMonth(-1)
.toISODateString()
);
console.log(
'End of last month: ', today.toStartOfMonth()
.toDateFromDays(-1)
.toISODateString()
);
console.log(
'Start of next month: ', today.toStartOfMonth(1)
.toISODateString()
);
console.log(
'End of next month: ', today.toStartOfMonth(2)
.toDateFromDays(-1)
.toISODateString()
);

查看 MDN documentation for the Date constructor function有关此示例中使用的 native 方法的更多信息。

关于JavaScript:如何获取 7 天前、上个月、本周的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37567569/

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