Getting first and last day of three consecutive month in javascript - javascript

I want to get the first date and last date of three consecutive month starting from current moth and moving towards past,i.e. if current month is November,my output will be first and last of Nov,followed by first and last date of October,followed by first and last date of September. I have written the code as follows:
<script type="text/javascript">
function calculatingMonthRange(){
console.log('calculatingMonthRange() got called');
var date = new Date();
var currdate=date;
for(i=1;i<=3;i++){
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
console.log('FirstDay='+firstDay+' LastDay='+lastDay+"\n");
date.setDate(currdate.getDate()-(30*i));
}
}
</script>
this is the output what I'm getting:::
FirstDay=Fri Nov 01 2013 00:00:00 GMT+0530 (India Standard Time) LastDay=Sat Nov 30 2013 00:00:00 GMT+0530 (India Standard Time)
FirstDay=Tue Oct 01 2013 00:00:00 GMT+0530 (India Standard Time) LastDay=Thu Oct 31 2013 00:00:00 GMT+0530 (India Standard Time)
FirstDay=Thu Aug 01 2013 00:00:00 GMT+0530 (India Standard Time) LastDay=Sat Aug 31 2013 00:00:00 GMT+0530 (India Standard Time)
where is the month September is going????why is it printing August instead of September?????

This works:
function calculatingMonthRange(){
console.log('calculatingMonthRange() got called');
var date = new Date();
var currdate=date;
for(i=1;i<=3;i++){
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
console.log('FirstDay='+firstDay+' LastDay='+lastDay+"\n");
date = new Date(date.getFullYear(), date.getMonth() - 1, 1);
}
}
You can play with the code here: http://jsbin.com/uPaVOXI/1/edit?js,console

Related

How to change time like 23:59:59 in React

I want to make to time like 23:59:59. Now it's printing 05 2022 00:00:00 and my requirement is Sat Nov 05 2022 23:59:59.
var now = new Date();
var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
var lastSunday = new Date(today.setDate(today.getDate()-today.getDay()));
var from = new Date(lastSunday.getTime() - (7*24*60*60*1000));
var to = new Date(lastSunday.getTime() - (1*24*60*60*1000));
Output:
console.log(now.toString()) //today Thu Nov 10 2022 13:20:22 GMT+0530 (India Standard Time)
console.log(from.toString()) // Sun Oct 30 2022 00:00:00 GMT+0530 (India Standard Time)
console.log(to.toString()) //Sat Nov 05 2022 00:00:00 GMT+0530 (India Standard Time)
hope this will help you!!
const myFunction = () => {
var now = new Date();
var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
var lastSunday = new Date(today.setDate(today.getDate() - today.getDay()));
var temp = new Date(lastSunday.getTime() - 1000);
document.getElementById("temp").innerHTML = temp;
};
<body onload="myFunction()">
<h1 id="temp"></h1>
</body>

How to convert date from react calendar in the dd/mm/yyyy format?

I am using react-calendar , Here I am getting a date in the following format
Wed Feb 02 2022 00:00:00 GMT+0530 (India Standard Time)
Now I am trying to convert it to dd/mm/yyyy. is there any way though which I can do this ?
Thanks.
The native Date object comes with seven formatting methods. Each of these seven methods give you a specific value -
toString() : Fri Jul 02 2021 14:03:54 GMT+0100 (British Summer Time)
toDateString(): Fri Jul 02 2021
toLocaleString() : 7/2/2021, 2:05:07 PM
toLocaleDateString() : 7/2/2021
toGMTString() : Fri, 02 Jul 2021 13:06:02 GMT
toUTCString() : Fri, 02 Jul 2021 13:06:28 GMT
toISOString() : 2021-07-02T13:06:53.422Z
var date = new Date();
// toString()
console.log(date.toString());
// toDateString()
console.log(date.toDateString());
// toLocalString()
console.log(date.toLocaleString());
// toLocalDateString()
console.log(date.toLocaleDateString());
// toGMTString()
console.log(date.toGMTString());
// toGMTString()
console.log(date.toUTCString());
// toGMTString()
console.log(date.toISOString());
Format Indian Standard time to Local time -
const IndianDate = 'Wed Feb 02 2022 00:00:00 GMT+0530 (India Standard Time)';
const localDate = new Date(IndianDate).toLocaleDateString();
console.log(localDate);
You could use the methods shown in this blogpost https://bobbyhadz.com/blog/javascript-format-date-dd-mm-yyyy from Borislav Hadzhiev.
You could a new date based on your calendar date and afterwards format it:
function padTo2Digits(num) {
return num.toString().padStart(2, '0');
}
function formatDate(date) {
return [
padTo2Digits(date.getDate()),
padTo2Digits(date.getMonth() + 1),
date.getFullYear(),
].join('/');
}
console.log(formatDate(new Date('Wed Feb 02 2022 00:00:00 GMT+0530 (India Standard Time)')));
This is JavaScript default date format.
You can use libraries like momentjs, datefns, etc to get the result.
For example, if you are using momentjs:-
moment(date).format('dd/mm/yyyy);
Or if you don't want to use any third-party library you can get the result from JavaScript's default date object methods.
const date = new Date();
const day = date.getDate() < 10 ? 0${date.getDate()} : date.getDate();
const month = date.getMonth() + 1 < 10 ? 0${date.getMonth() + 1} : date.getDate() + 1;
const year = date.getFullYear();
const formattedDate = ${day}/${month}/${year};

Why is getDate() a month off?

I'm trying to iterate over objects that have a date within a specific time frame. These time frames are days in the past week bounded by startDate and endDate.
I have a for loop that sets the startDate to the beginning of the day, and endDate will be the end of the day.
let startDate = new Date(); // startDate: Start bound of average execution time calculation
let endDate = new Date(); // endDate: End bound of average execution time calculation
for (let i = 0; i < 7; i++) {
// i is used to decrement the startDate to i days from today
// set startDate to beginning of the day
startDate = new Date();
startDate.setDate(startDate.getDate() - i);
startDate.setHours(0, 0, 0, 0);
console.log(i + ": " + startDate);
// set endDate to end of the day
endDate.setDate(startDate.getDate());
endDate.setHours(23, 59, 59, 999);
console.log(i + ": " + endDate);
However, even though I'm setting the endDate to the startDate, the console tells me that the endDate is one month off. Why is this happening?
The console shows this:
0: Sat Aug 01 2020 00:00:00 GMT-0700 (Mountain Standard Time)
0: Sat Aug 01 2020 23:59:59 GMT-0700 (Mountain Standard Time)
1: Fri Jul 31 2020 00:00:00 GMT-0700 (Mountain Standard Time)
1: Mon Aug 31 2020 23:59:59 GMT-0700 (Mountain Standard Time)
2: Thu Jul 30 2020 00:00:00 GMT-0700 (Mountain Standard Time)
2: Sun Aug 30 2020 23:59:59 GMT-0700 (Mountain Standard Time)
3: Wed Jul 29 2020 00:00:00 GMT-0700 (Mountain Standard Time)
3: Sat Aug 29 2020 23:59:59 GMT-0700 (Mountain Standard Time)
4: Tue Jul 28 2020 00:00:00 GMT-0700 (Mountain Standard Time)
4: Fri Aug 28 2020 23:59:59 GMT-0700 (Mountain Standard Time)
5: Mon Jul 27 2020 00:00:00 GMT-0700 (Mountain Standard Time)
5: Thu Aug 27 2020 23:59:59 GMT-0700 (Mountain Standard Time)
Think like arrays, where the first element is 0. The same is true here. The months of the year will be 0 - 11, rather than 1 - 12. The getMonth() method returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year).
Also, keep this in mind: With setDate() the expected values are 1-31, but other values are allowed: 0 will result in the last day of the previous month. -1 will result in the day before the last day of the previous month.
So, I ended up just fixing it by not referring to startDate at all. I'm not sure why I can't call getDate() without getting the wrong date, but for now, I'll just set endDate by repeating whatever I did to startDate.
// set startDate to beginning of the day
startDate = new Date();
startDate.setDate(startDate.getDate() - i);
startDate.setHours(0, 0, 0, 0);
console.log(i + ": " + startDate);
// set endDate to end of the day
endDate = new Date();
endDate.setDate(endDate.getDate() - i);
endDate.setHours(23, 59, 59, 59);
console.log(i + ": " + endDate);
The reason it happens is because you are setting the date but not the month, so when the loop crosses a month boundary, the start goes to the previous month but end stays in the current month.
When run on 1 Aug, in the first iteration both start and end are set to 1 Aug.
In the next iteration, 1 is subtracted from the start so it's 31 July, but then just the date is set for end, so it goes to 31 Aug, and so on…
A reworking of the code:
// Start at random time on 1 Aug
let d = new Date(2020,7,1,15,23,51,3);
for (let start, end, i=0; i<7; i++) {
start = new Date(d.setHours(0,0,0,0));
console.log(i + ' : ' + start.toString());
end = new Date(d.setHours(23,59,59,999));
console.log(i + ' : ' + end.toString());
d.setDate(d.getDate() - 1);
}

How to use .replace to replace the string in a array

need to replace GMT+0530 (India Standard Time) to IST
dynamically for multiple array list
for now my array list has 6 entries. need to replace for all the array list .
function getTimeAccordingtoTimeZone(utc){
utc = new Date(Date.parse(utc));
var dateUTC = utc ;
var dateIST = new Date(dateUTC);
//date shifting for IST timezone (+5 hours and 30 minutes)
var current_time_zone = getCurrentTimeZone();
var hour_diff = parseInt(current_time_zone);
var minute_diff = current_time_zone - hour_diff;
minute_diff = minute_diff*60;
dateIST.setHours(dateIST.getHours() + hour_diff);
dateIST.setMinutes(dateIST.getMinutes() + minute_diff);
var new_date = dateIST;
return new_date;
}
new_date returns
Tue Jan 15 2019 22:49:04 GMT+0530 (India Standard Time)
Tue Jan 15 2019 22:49:04 GMT+0530 (India Standard Time)
Tue Jan 15 2019 22:49:04 GMT+0530 (India Standard Time)
Tue Jan 15 2019 22:49:04 GMT+0530 (India Standard Time)
Tue Jan 15 2019 22:49:04 GMT+0530 (India Standard Time)
Tue Jan 15 2019 22:49:04 GMT+0530 (India Standard Time)
I'd propose you use moment JS to format the string.
In your case, the following code will help you:
const moment = require('moment');
date = moment();
const dateString = `${date.format('ddd MMM DD YYYY HH:mm:ss')} IST`
console.log(dateString);
MomentJs Documentation

31 days in February in Date object

This code should log all days for given month:
var date = new Date(2012,2,1);
var thisMonth = date.getMonth();
while(date.getMonth()==thisMonth) { // 31 steps ???
console.log(date.getMonth(),date.getDate());
date.setDate(date.getDate()+1);
}
It works well for every month but February. Any ideas where is the catch?
Note the month parameter is 0-indexed, so your code is about March not February.
The doc:
month
Integer value representing the month, beginning with 0 for January to
11 for December.
Use new Date(2012,1,1); month is zero-based ;-)
This is pretty interesting:
new Date('2014-02-28'); // Fri Feb 28 2014 01:00:00 GMT+0100
new Date('2014-02-29'); // Sat Mar 01 2014 01:00:00 GMT+0100
new Date('2014-02-30'); // Sun Mar 02 2014 01:00:00 GMT+0100
new Date('2014-02-31'); // Mon Mar 03 2014 01:00:00 GMT+0100
new Date('2014-02-32'); // Invalid Date

Categories

Resources