This month (March) has 31 days.
I want to get the last day of the month and instead of get Wed Mar 31 2021 23:59:59 I get Fri Apr 30 2021 23:59:59 look:
let d = new Date()
d.setMonth( d.getMonth() + 1) // April
d.setDate(0) // should bring the 31 of March
d.setHours(23, 59, 59, 999)
console.log(d) // Fri Apr 30 2021 23:59:59 GMT+0300 (IDT)
Why does it happen on date with 31 days?
When tried on different months every month it worked as well, for example:
let d = new Date("2021-02-25") // notice that we point to February
d.setMonth( d.getMonth() + 1)
d.setDate(0)
d.setHours(23, 59, 59, 999)
console.log(d) // Sun Feb 28 2021 23:59:59 GMT+0200 (IST)
Notice that in the second example - which is working good, we get the last day of Feb and GMT+2 (IST) and not GMT+3 (IDT)
Also notice that if I declare it like that: let d = new Date('2021-03-25') it also works good (with specific date, instead of just new Date())
It happens because April only has 30 days.
let d = new Date()
d.setMonth( d.getMonth() + 1) // Actually April 31st -> May 1st.
Try this way:
d.setMonth(d.getMonth(), 0);
second argument 0 will result in the last day of the previous month
Got it!
I set +1 for the month while the current date is 31 and what will happen is that it will jump to 31 of April which doesn't exist and the default date will be 1 in May.
So prev date of 1 in May is 30 of April.
I should set the date to 1 before doing the increment of the month, look:
let d = new Date()
d.setDate(1) // this is the change - important!
d.setMonth( d.getMonth() + 1)
d.setDate(0)
d.setHours(23, 59, 59, 999)
console.log(d) // Wed Mar 31 2021 23:59:59
That way, it will start from 1 of March, inc to 1 of April, and go prev date to last day of March.
Even that it also works, weird:
var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1, 0, 0, 0, 0);
var lastDay = new Date(y, m + 1, 0, 23, 59, 59, 999)
console.log(lastDay)
Related
var today = new Date();
var endYear = new Date(1995, 11, 31, 23, 59, 59, 999); // Set day and month
endYear.setFullYear(today.getFullYear()); // Set year to this year
console.log("Version 1: end year full date is ", endYear);
var msPerDay = 24 * 60 * 60 * 1000; // Number of milliseconds per day
var daysLeft = (endYear.getTime() - today.getTime()) / msPerDay;
var daysLeft = Math.round(daysLeft); //returns days left in the year
console.log(daysLeft,endYear);
// when l write that code answer is 245.
var today = new Date();
var endYear = new Date(2021, 0, 0, 0, 0, 0, 0); // Set day and month
console.log("Version 2: end year full date is ", endYear);
var msPerDay = 24 * 60 * 60 * 1000; // Number of milliseconds per day
var daysLeft = (endYear.getTime() - today.getTime()) / msPerDay;
var daysLeft = Math.round(daysLeft); //returns days left in the year
console.log(daysLeft,endYear);
// but when l add only 1 ms then answer returns like 244. but how is it possible? where has 1 day gone?
That is the difference with the time you set.
To be clear,
first endYear will print Thu Dec 31 2020 23:59:59
second endYear will print Thu Dec 31 2020 00:00:00
That is the difference you see there.
I will post the complete out put I received on console here as well.
Thu Dec 31 2020 23:59:59 GMT+0530 (India Standard Time)
245.0131708912037
245
Thu Dec 31 2020 00:00:00 GMT+0530 (India Standard Time)
244.01317090277777
244
==================EDIT==================
new Date(2021, 0, 0, 0, 0, 0, 0) calculates this to Dec 31st because date is indexed from 1 and not zero. If that value is zero it computes it as the day before the 31st of December.
For example,
new Date(Date.UTC(2021, 1, 0, 0, 0, 0, 0)) will print out Sat Jan 31 2021 05:30:00 GMT+0530 (India Standard Time)
and
new Date(Date.UTC(2021, 1, -1, 0, 0, 0, 0)) will print out Sat Jan 30 2021 05:30:00 GMT+0530 (India Standard Time)
I've seen that code-technique, trick, hack (how you wanna call it) on CodeReview: https://codereview.stackexchange.com/questions/142706/take-a-specified-weekday-and-check-if-it-falls-on-the-remaining-days-of-the-cur
In the 6th line the getDate() method is used for to get the count of days of a month. Month specified as the previous parameter.
I've played around with these technique and it seems to work:
var d = new Date();
var sep = new Date(d.getYear(), (d.getMonth() + 1), 0).getDate();
var oct = new Date(d.getYear(), (d.getMonth() + 2), 0).getDate();
var nov = new Date(d.getYear(), (d.getMonth() + 3), 0).getDate();
var dec = new Date(d.getYear(), (d.getMonth() + 4), 0).getDate();
var jan = new Date(d.getYear(), (d.getMonth() + 5), 0).getDate();
console.log(d.toLocaleString('en-US', { month: 'long' }));
console.log('%s %s %s %s %s', sep, oct, nov, dec, jan);
But how is it possible that it works?
I would expect the Date-constructor to accept only valid integers.
One can give it whatever integer one likes. It doesn't throw an exception. BUT: The returned values are scrap.
var d = new Date();
var nov = new Date(d.getYear(), (d.getMonth() + 3), 31).getDate(); // November has 30 days.
console.log('%s', nov); // => 31
var nov = new Date(d.getYear(), (d.getMonth() + 3), -21).getDate(); // November has 30 days.
console.log('%s', nov); // 9
var nov = new Date(d.getYear(), (d.getMonth() + 3), 301).getDate(); // November has 30 days.
console.log('%s', nov); // 27
Can anyone with some insights explain what goes on there?
This is simply a property of the Date class, as documented on MDN:
Where Date is called as a constructor with more than one argument, if values are greater than their logical range (e.g. 13 is provided as the month value or 70 for the minute value), the adjacent value will be adjusted. E.g. new Date(2013, 13, 1) is equivalent to new Date(2014, 1, 1), both create a date for 2014-02-01 (note that the month is 0-based). Similarly for other values: new Date(2013, 2, 1, 0, 70) is equivalent to new Date(2013, 2, 1, 1, 10) which both create a date for 2013-03-01T01:10:00.
They talk about values greater than their logical range, but the same logic applies for values lower than their range.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
var d = new Date();
var nov = new Date(d.getYear(), (d.getMonth() + 3), 31).getDate(); // November has 30 days.
console.log('%s', nov); // => 31 Thu Dec 31 2016 00:00:00 GMT+0530 (India Standard Time)
var nov = new Date(d.getYear(), (d.getMonth() + 3), -21).getDate(); // November has 30 days.
console.log('%s', nov); // 9 Mon Nov 09 2016 00:00:00 GMT+0530 (India Standard Time)
var nov = new Date(d.getYear(), (d.getMonth() + 3), 301).getDate(); // November has 30 days.
console.log('%s', nov); // 27 Mon Sep 27 2017 00:00:00 GMT+0530 (India Standard Time)
I have Date Object ,I wanted to clear HOUR,MINUTE and SECONDS from My Date.Please help me how to do it in Javascript. Am i doing wrong ?
var date = Date("Fri, 26 Sep 2014 18:30:00 GMT");
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
Expected result is
Fri, 26 Sep 2014 00:00:00 GMT
How Do I achieve ?
According to MDN the setHours function actually takes additional optional parameters to set both minutes, seconds and milliseconds. Hence we may simply write
// dateString is for example "Fri, 26 Sep 2014 18:30:00 GMT"
function getFormattedDate(dateString) {
var date = new Date(dateString);
date.setHours(0, 0, 0); // Set hours, minutes and seconds
return date.toString();
}
You can use this:
// Like Fri, 26 Sep 2014 18:30:00 GMT
var today = new Date();
var myToday = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0);
Recreate the Date object with constructor using the actual date.
To parse the date into JavaScript simply use
var date = new Date("Fri, 26 Sep 2014 18:30:00 GMT”);
And then set Hours, Minutes and seconds to 0 with the following lines
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
date.toString() now returns your desired date
Look at this code:
var date = new Date();
console.log(date);
// Tue Apr 30 2013 14:24:49 GMT+0430
var date2 = new Date(
date.getFullYear(),
date.getMonth(),
date.getDay(), 0, 0, 0, 0
)
console.log(date2)
// Tue Apr 02 2013 00:00:00 GMT+0430
I simply extracted some date from today's date, and created another date with that data, and the result is another date, not today.
What's wrong with JavaScript's Date object?
.getDay() returns the day of the week (0-6), not day of the month. (It returns 2 for Tuesday)
Use getDate() - it will return 30
getDay() returns the day of the week (from 0 to 6), not the day of the month (1-31).
the correct method is getDate():
var date = new Date();
console.log(date);
// Tue Apr 30 2013 14:24:49 GMT+0430
var date2 = new Date(
date.getFullYear(),
date.getMonth(),
date.getDate(), 0, 0, 0, 0
)
console.log(date2)
// Tue Apr 30 2013 00:00:00 GMT+0430
How can I obtain the last day of the month with the timestamp being 11:59:59 PM?
function LastDayOfMonth(Year, Month) {
return new Date((new Date(Year, Month, 1)) - 1);
}
console.log(LastDayOfMonth(2009, 11))
Example:
> LastDayOfMonth(2009, 11)
Mon Nov 30 2009 23:59:59 GMT+0100 (CET)
This will give you last day of current month.
var t= new Date();
alert(new Date(t.getFullYear(), t.getMonth() + 1, 0, 23, 59, 59));
var d = new Date();
console.log(d);
d.setMonth(d.getMonth() + 1); // set month as next month
console.log(d);
d.setDate(0); // get the last day of previous month
console.log(d);
Here is output from the code above:
Thu Oct 03 2013 11:34:59 GMT+0100 (GMT Daylight Time)
Sun Nov 03 2013 11:34:59 GMT+0000 (GMT Standard Time)
Thu Oct 31 2013 11:34:59 GMT+0000 (GMT Standard Time)
var d = new Date();
m = d.getMonth(); //current month
y = d.getFullYear(); //current year
alert(new Date(y,m,1)); //this is first day of current month
alert(new Date(y,m+1,0)); //this is last day of current month
var month = 1; // 1 for January
var d = new Date(2015, month, 0);
console.log(d); // last day in January
Sometimes all you have is a text version of the current month, ie: April 2017.
//first and last of the current month
var current_month = "April 2017";
var arrMonth = current_month.split(" ");
var first_day = new Date(arrMonth[0] + " 1 " + arrMonth[1]);
//even though I already have the values, I'm using date functions to get year and month
//because month is zero-based
var last_day = new Date(first_day.getFullYear(), first_day.getMonth() + 1, 0, 23, 59, 59);
//use moment,js to format
var start = moment(first_day).format("YYYY-MM-DD");
var end = moment(last_day).format("YYYY-MM-DD");
Last day of the month
now = new Date
lastDayOfTheMonth = new Date(1900+now.getYear(), now.getMonth()+1, 0)
Most of these answers are missing one thing or another. After playing with most of them I came up with the following that gives you the last possible millisecond of the month.
let testDate = new Date();
console.log(getMonthEnd(testDate));
function getMonthEnd(value) {
return new Date(value.getFullYear(), value.getMonth() + 1, 0, 23, 59, 59, 999);
}
Probably missing something in this one too but seems to cover all my requirements.
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
Date lastDayOfMonth = cal.getTime();
Do not forget month started with 0 so +1 in month too.
let enddayofmonth = new Date(year, month, 0).getDate();