本文介绍了在javascript中查找上个月的第一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定日期对象,如何在javascript中获取上个月的第一天
Given a date object, how to get its previous month's first day in javascript
推荐答案
function firstDayInPreviousMonth(yourDate) {
var d = new Date(yourDate);
d.setDate(1);
d.setMonth(d.getMonth() - 1);
return d;
}
编辑:好吧......我肯定在这里学到了一些东西。我认为这是涵盖所有情况的最简单的解决方案(是的,它适用于1月):
Alright... I've definitely learned something here. I think that this is the simplest solution that covers all cases (and yes, it does work for January):
function firstDayInPreviousMonth(yourDate) {
return new Date(yourDate.getFullYear(), yourDate.getMonth() - 1, 1);
}
这篇关于在javascript中查找上个月的第一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!