简体   繁体   English

获取JQuery中上个月的第一个和最后一个日期

[英]Get the first and last date of the previous month in JQuery

I have this script,我有这个脚本,

 var today = new Date();
 var dd = today.getDate();
 var ddd = today.getDate()-1;
 var dddd = today.getDate()-2;

 var mm = today.getMonth()+1; //January is 0!
 var yyyy = today.getFullYear();
 if(dd<10){
   dd='0'+dd
 } 
 if(mm<10){
   mm='0'+mm
 } 
 if(ddd<10){
   ddd='0'+ddd
 } 

 var today = dd+'/'+mm+'/'+yyyy;
 var d2 = ddd+'/'+mm+'/'+yyyy;
 var d3 = dddd+'/'+mm+'/'+yyyy;

With this i obtain the last 3 days of the current day but in this case today is 02 if i rest two days i obtain 0 but i want in this case the last day of the previous month, how can do this?有了这个,我获得了当天的最后 3 天,但在这种情况下,今天是 02,如果我 rest 两天我获得 0,但在这种情况下我想要上个月的最后一天,怎么做?

Here is my fiddle这是我的小提琴

That's the first day of the current month new Date(now.getFullYear(), now.getMonth(), 1) to get the last day of the previous month create a date 1-day earlier: new Date(now.getFullYear(), now.getMonth(), 1 - 1) .那是当月的第一天new Date(now.getFullYear(), now.getMonth(), 1)获取上个月的最后一天 创建一个提前 1 天的日期: new Date(now.getFullYear(), now.getMonth(), 1 - 1)

To get the first day of the previous month we should substract 1 from the month component new Date(now.getFullYear(), now.getMonth() - 1, 1) but there is an issue if the current month is January (0) and the previous month is December (11).要获得上个月的第一天,我们应该从月份组件new Date(now.getFullYear(), now.getMonth() - 1, 1)但如果当前月份是 1 月 (0),则会出现问题上个月是 12 月 (11)。 Hence I wrapped the month expression creating a cycle so it always returns a positiove value.因此,我将月份表达式包装成一个循环,因此它总是返回一个正值。

 var now = new Date(); var prevMonthLastDate = new Date(now.getFullYear(), now.getMonth(), 0); var prevMonthFirstDate = new Date(now.getFullYear() - (now.getMonth() > 0 ? 0 : 1), (now.getMonth() - 1 + 12) % 12, 1); var formatDateComponent = function(dateComponent) { return (dateComponent < 10 ? '0' : '') + dateComponent; }; var formatDate = function(date) { return formatDateComponent(date.getMonth() + 1) + '/' + formatDateComponent(date.getDate()) + '/' + date.getFullYear(); }; document.write(formatDate(prevMonthFirstDate) + ' - ' + formatDate(prevMonthLastDate));

Try using bellow尝试使用波纹管

var date = new Date();
var monthStartDay = new Date(date.getFullYear(), date.getMonth(), 1);
var monthEndDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);

You can do it like this你可以这样做

 //one day previous Date var date = new Date(); date.setDate(date.getDate() - 1); console.log(date.getDay()); //for previous month last date var date = new Date(); date.setDate(0); console.log(date); //for perivous Month First date var date = new Date(); date.setDate(0); date.setDate(1); console.log(date);

Try this: get today's date and set date to 1 which will get first of the current month.试试这个:获取今天的日期并将日期设置为 1,这将获得当月的第一个日期。 Then set hour to -1 which will shift it to previous months last day.然后将小时设置为 -1,这将在最后一天将其移至前几个月。 Then set day to 1 to get first day of previous month.然后将 day 设置为 1 以获取上个月的第一天。

var today = new Date();
var dd = today.getDate();

today.setDate(1); // going to 1st of the month
today.setHours(-1); // going to last hour before this date even started.
var lastDay = today.toLocaleDateString('en-GB', {  
                              year: 'numeric',
                              month: 'numeric',
                              day: 'numeric'
                          }).split(' ').join('-');

alert("last day of previous month " + lastDay);

today.setDate(1); // going to 1st of the previous month

var firstDay = today.toLocaleDateString('en-GB', {  
                              year: 'numeric',
                              month: 'numeric',
                              day: 'numeric'
                          }).split(' ').join('-');

alert("first day of previous month " + firstDay);

JSFiddle Demo JSFiddle 演示

What I have guessed is if current month is February then your output should be 1,31 because January's first day and last day is 1 and 31. So if that is the case then我猜测的是,如果当前月份是二月,那么您的输出应该是1,31因为一月的第一天和最后一天是 1 和 31。所以如果是这样的话

var toDay = new Date();
var myDate = new Date(2016,toDay.getMonth()-1);
Fri Jan 01 2016 00:00:00 GMT+0530 (India Standard Time)

This will be assigned with the first day of the previous month.这将被分配为上个月的第一天。

Also, for the second case另外,对于第二种情况

var myDate = new Date(2016, toDay.getMonth());
myDate = new Date(myDate -1);
Sat Jan 31 2015 23:59:59 GMT+0530 (India Standard Time)

You can just use myDate to find whatever you want.您可以使用myDate来查找您想要的任何内容。

A convenient trick to get the end of last month is the take current date and use setDate(0) .获取上个月月底的一个方便技巧是获取当前日期并使用setDate(0) Then use that that to create a new date object and setDate(1) for beginning of the month然后使用它来创建一个新的日期对象和setDate(1)为月初

 var prevMonthEnd = new Date(); prevMonthEnd.setDate(0); var beginLastMonth = new Date(prevMonthEnd); beginLastMonth.setDate(1); console.log([beginLastMonth.toString(), prevMonthEnd.toString()])

 var firstDay = new Date(); var secondDay = new Date(); var thirdDay = new Date(); //Now say you want to set secondDay as before 2 days secondDay.setDate(secondDay.getDate()-2); //Now say you want to set thirdDay as after 2 days thirdDay.setDate(thirdDay.getDate()+2); alert("firstDay: "+firstDay+", secondDay:"+secondDay+", thirdDay:"+thirdDay); alert("date of thirdDay: dd/mm/yyyy "+thirdDay.getDate()+"/"+ (thirdDay.getMonth()+1) +"/"+thirdDay.getFullYear());

I know many people have already answered it but I just figure out a 3 line solution so I thought I could share it in case anyone needs it.我知道很多人已经回答了这个问题,但我只是想出了一个3 行解决方案,所以我想我可以分享它以防万一有人需要它。

 let date = new Date(); let firstDay = new Date(date.getFullYear(), date.getMonth() - 1, 2); let lastDay = new Date(date.getFullYear(), date.getMonth(), 1); console.log(firstDay.toISOString(), lastDay.toISOString())

This solution works dynamically even if the current month is January , this method will auto-detect the previous year.即使当前月份是一月,此解决方案也会动态工作,此方法将自动检测上一年。

Example:例子:

If the current date is: 2022-01-12如果当前日期是: 2022-01-12

Previous month's first day: 2021-12-01上个月的第一天: 2021-12-01

Previous month's last day: 2021-12-31上个月最后一天: 2021-12-31

**Additionally you can set the time of both dates to 00:00:00 by using setUTCHours like this: **此外,您可以使用setUTCHours将两个日期的时间设置为00:00:00 ,如下所示:

 let date = new Date(); let firstDay = new Date(date.getFullYear(), date.getMonth() - 1, 2); firstDay.setUTCHours(0,0,0,0); let lastDay = new Date(date.getFullYear(), date.getMonth(), 1); lastDay.setUTCHours(0,0,0,0); console.log(firstDay.toISOString(), lastDay.toISOString())

Here's a working Fiddle example in case you need it.如果您需要,这是一个有效的Fiddle示例。

var mydate = new Date(2020,2,1); // march 1st 2020 leap year
var lastdate = new Date(mydate.getFullYear(),mydate.getMonth(),0);// get last date of prev month
var firstdate = new Date(lastdate.getFullYear(), lastdate.getMonth(), 1);//get first date using last date

alert('' + firstdate.toDateString() + '');
alert('' + lastdate.toDateString() + '');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每当我在 jquery ui datepicker 中更改月/年时,如何获取该月的第一个日期和最后一个日期 - how to get the first date and last date of the month whenever I change the month/year in jquery ui datepicker 如何获取上周的第一个和最后一个日期? - How to get the first and last date of the previous week? MomentJS - 如何从日期获取上个月的最后一天? - MomentJS - How to get last day of previous month from date? 如何在javascript中从月份和年份中获取第一个日期和最后一个日期 - how to get first date and last date from month and year in javascript 将字符串转换为日期时,将给定月份的第一天转换为上个月的最后一天 - While converting string to date, the first day of a given month is being converted into the last day of the previous month 如何在 Javascript 或 JQuery 中获取上个月的最后一天 - How to get the last day of the previous month in Javascript or JQuery 上个月最后一天的javascript日期 - javascript date of last day of previous month 如何在Java脚本或jQuery中获取上个月的日期 - How to get last month date in java script or jquery 获取本月的第一天和最后一天 - get the first and last day of the month Date.UTC()返回上个月的最后一天,而getUTCFullYear(),getUTCMonth()正确返回正确月份的第一天 - Date.UTC() returns last day of previous month while getUTCFullYear(), getUTCMonth() returns correctly first day of correct month
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM