How to get start date and end date of month in Javascript? - javascript

I have only month e.g : 'May'
from this i want to get start date and last date of 'May' month using java-script.
Thank you.

The first date of any month would be 1. As far as the last date is concerned you can use moveToLastDayOfMonth
Moves the date to the last day of the month.
or you can use this way:
var month = 4;
var d = new Date(2016, month + 1, 0);
alert(d);

try this:
date = new Date(2016, 5, 1);
lastdate = new Date(date - 1);
console.log(lastdate)

start date always will be 1 and,
for last date you can try this,
var date = new Date();
var lastDate = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();

Thank you for your respond.
I found this answer.
var month = 'MAY'
var a = '1-'+month+'-2015';
var date = new Date(a);
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
console.log(firstDay);
console.log(lastDay);

I created months array for check month value for JS standard date.Then get date format Date(year,month,day) as you want
var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
var month = "May";
for(var i in months){
if(months[i] == month){
month = i;
}
}
var s = new Date(2016, month, 1);
var e = new Date(2016, ++month, 0);
console.log(s+"\n"+e);

Related

JS Get day next month starts on

How can I get the day that the next month starts on? (Monday Tuesday etc.).
I tried the following but it returns more than just the day.
var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);
You're almost there, just add this after your last line of code to get the name of the day
var dayName = lastDay.toLocaleDateString('en-us', { weekday: 'long' });
Also keep in mind that Date constructor's second parameter is monthIndex MDN.
You should add m by 1 to get next month's first day and add it by 2 and provide date = 0 to get the last day of the next month.
var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m + 1, 1);
var lastDay = new Date(y, m + 2, 0);
var firstDayName = firstDay .toLocaleDateString('en-us', { weekday: 'long' });
var lastDayName = lastDay.toLocaleDateString('en-us', { weekday: 'long' });
console.log(firstDayName, lastDayName)
Your current code is getting Date instances for the first and last day of the current month. So first you'll need to fix that. Then, if you want day numbers instead, you have to call getDay to get the values (0 = Sunday through 6 = Saturday):
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth() + 1; // + 1 = next month
const firstDay = new Date(year, month, 1).getDay();
const lastDay = new Date(year, month + 1, 0).getDay();
console.log({firstDay, lastDay});
As I write this on July 31st, that shows 1 and 3, telling us that August 2022 starts on Monday and ends on Wednesday.
I just took the current date and got the first day of the month. Then we just converted this to string. It's working. Try this:
const date = new Date(); // current date 1/8/2022
const nextMonth = new Date(date.getFullYear(), date.getMonth() + 1, 1); //first day of next month
const nextMonthStartDay = nextMonth.toLocaleDateString('en-US', { weekday: 'long' }); //convert to date string
console.log(nextMonthStartDay) //Thursday

How to get last date of current month, and next month when condition apply

I can get the current end of month date by below javascript. However, I get stuck on setting condition with it.
For instance, if the input date is after 21st of the current month, the lastDay will be the last day of next month.
var date = new Date();
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
There are various possiblities. For instance
var date = new Date();
var lastday;
if (date.getDate() > 21)
lastday = new Date(date.getFullYear(), date.getMonth() + 2, 0);
else
lastday = new Date(date.getFullYear(), date.getMonth() + 1, 0);
or using the ternary operator for assignment
var date = new Date();
var lastday = date.getDate() > 21
? new Date(date.getFullYear(), date.getMonth() + 2, 0)
: new Date(date.getFullYear(), date.getMonth() + 1, 0);
or calculating the monthOffset beforehand
var date = new Date();
var monthOffset = 1;
if (date.getDate() > 21)
monthOffset = 2;
var lastday = new Date(date.getFullYear(), date.getMonth() + monthOffset, 0);
or calculating the monthOffset beforehand using a ternary operator
var date = new Date();
var monthOffset = date.getDate() > 21 ? 2: 1
var lastday = new Date(date.getFullYear(), date.getMonth() + monthOffset, 0);
or calulating the monthOffset inline without additional variable
var date = new Date();
var lastday = new Date(date.getFullYear(), date.getMonth() + (date.getDate() > 21 ? 2 : 1), 0);
They are all based on the same principle: Set the number of months to add to either 1 or 2, based on the current date. For terms of readability, I would probably prefer the variant 4.
var date = new Date();
var condition = date.getDate() > 21;
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1 + (condition ? 1 : 0), 0);
If the current date is after the 21st, if will return the end of the next month, otherwise it will return the end of the current month.

Manipulating Date/time with JS and moment

I have 2 dates and I need to create a 3rd date using those 2 dates.
if (document.getElementById("endDate"))
endDate = document.getElementById("endDate").value;
if (document.getElementById("presentDate"))
presentDate = document.getElementById("presentDate").value;
If Present date = "12/5/2018" and End date = "12/25/2018" then my New date = "12/26/2018";
Since, JavaScript date months range from 0-11 and also the dates are kind of messy, I am not getting the desired results.
What I tried:
var presentDt = new Date(presentDate);
var endDt = new Date(endDate);
var newDay = endDt.getUTCDate()+1;
var presentMonth = presentDt.getUTCMonth();
var presentYear = presentDt.getUTCFullYear();
var nextDate= presentMonth + '/' + endDay + '/' + presentYear;
Issue 1: This above code works but if my endDate is on the 31st, then adding UTCDate+1 makes it 32, which returns invalid date.
Issue 2: If I do UTCMonth(), it returns 11 but if I want to add 2 months then it returns 13 which is also invalid. The issue is basically that I am not able to manipulate the dates as I want.
I also tried moment, however I am having similar issues and I am not able to manipulate the dates easily as I want them to.
Also tried getDate and getMonth but it does the same thing.
Is there a better way of handling the overall date/time with JavaScript?
1: add/subtract days:
new Date(new Date().setDate(new Date().getDay() + 3))
2: add/subtract months:
new Date(new Date().setMonth(new Date().getMonth() + 3))
//your code
var presentDt = new Date(presentDate);
var endDt = new Date(endDate);
var newDay = new Date(new Date().setDate(endDt.getDay() + 2));
var presentMonth = presentDt.getUTCMonth();
var presentYear = presentDt.getUTCFullYear();
Since your input string is not in a format recognized by new Date() across environments, I suggest to use moment(String, String) to parse it.
Then you can use moment(Object), moment's setters (like year(), month() and date()) and format() to get the desired output.
Here a live sample:
var presentDt = "12/5/2018";
var endDt = "12/25/2018";
// Parse your input with momentjs
var mPresent = moment(presentDt, "MM/DD/YYYY");
var mEnd = moment(endDt, "MM/DD/YYYY");
// Create a new momnt object compliant with your needs
var nextDate = moment({
year: mPresent.year(), // get presentDt's year
month: mPresent.month(), // get presentDt's month
date: mEnd.add(1, 'day').date() // get endDt day of the month and add 1 day to it
});
// Display the result in the format you need
console.log(nextDate.format("MM/DD/YYYY"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
Note that you will get 01 as day result when endDt represents the last day of the month.
This is what i did:
First i created the following functions to look at the number of days and months.
function formattedDate(date) {
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
return (monthIndex + 1) + '/' + day + '/' + year;
}
function myNewDate(datestring) {
var dates = datestring.split("/");
var year, month, day;
if (dates.length < 3) {
dates = datestring.split("-");
year = dates[0];
month = dates[1] - 1;
day = dates[2];
} else {
year = dates[2];
month = dates[0] - 1;
day = dates[1];
}
var days = [31, 28, 31, 30, 31, 30 ,31, 31, 30, 31, 30, 31];
// Overflow day
if (day > days[month])
day = days[month];
return new Date(year, month, day);
}
function newDate(date1, date2) {
var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var year = date1.getFullYear();
var month = date1.getMonth() + 1;
var day = date2.getDate() + 1;
// Check leap year
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
days[1] = 29;
// Overflow day
if (day > days[month])
day = days[month];
var newdate = new Date(year, month, day);
return newdate;
}
Here, i used the above function to create my new date. You can also add/subtract days/months as you want by updating the above.
var mpresent = myNewDate(presentDate);
var mstart = myNewDate(startDate);
var myDate = (formattedDate(newDate(mpresent, mstart)));

Current month (number) + year (number)

How do I output the current year and month (in numbers) in javascript?
getUTCMonth() logs: getUTCMonth is not defined
getUTCMonth() logs: getMonth is not defined
you could do it like
var date = new Date();
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear();
Attention
date.getMonth() starts with 0 and goes to 11 so you have to add +1
Did you try this ?
var date = new Date();
date.getUTCMonth();
date.getUTCFullYear();
Month:
new Date().getMonth()
Year:
new Date().getFullYear()

Get first and last date of current month with JavaScript or jQuery [duplicate]

This question already has answers here:
Calculate last day of month
(25 answers)
Closed 3 years ago.
As title says, I'm stuck on finding a way to get the first and last date of the current month with JavaScript or jQuery, and format it as:
For example, for November it should be :
var firstdate = '11/01/2012';
var lastdate = '11/30/2012';
Very simple, no library required:
var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
or you might prefer:
var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);
EDIT
Some browsers will treat two digit years as being in the 20th century, so that:
new Date(14, 0, 1);
gives 1 January, 1914. To avoid that, create a Date then set its values using setFullYear:
var date = new Date();
date.setFullYear(14, 0, 1); // 1 January, 14
I fixed it with Datejs
This is alerting the first day:
var fd = Date.today().clearTime().moveToFirstDayOfMonth();
var firstday = fd.toString("MM/dd/yyyy");
alert(firstday);
This is for the last day:
var ld = Date.today().clearTime().moveToLastDayOfMonth();
var lastday = ld.toString("MM/dd/yyyy");
alert(lastday);

Categories

Resources