Sabrilogesh K
Sabrilogesh K

Reputation: 307

How to get starting date and current date of month in react js?

I need to get the starting date and current date of the month to send the date in api to get data, is there any way to get the starting date of month and current date and simlarly year and week

eg : i need to send:

var apiUrl = window.$_APIurl+"admin/installStatistics?fromDate=2020-01-01&toDate=2020-07-03";
axios.get(apiUrl, {headers: headers})

This from date and to date is dynamic and i change it using dropdown selection , how to send this date in react js?

Upvotes: 0

Views: 3858

Answers (1)

xMayank
xMayank

Reputation: 1995

It is very simple.

var date = new Date("2024-12-10T02:00:00Z");
var firstDay = new Date(date.getFullYear(), date.getMonth(), 2);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 1);

var first = date.getDate() - date.getDay(); // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6

var firstDayofWeek = new Date(date.setDate(first)).toUTCString();
var lastdayOfWeek = new Date(date.setDate(last)).toUTCString();

console.log(firstDayofWeek)    
console.log(lastdayOfWeek)
console.log(firstDay)
console.log(lastDay)

Upvotes: 2

Related Questions