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

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>

Related

How do I find the nearest Past or Future Date in an array of Dates by an DateObject?

Let's say we have an Array of Dates
var dateArr = [new Date("Thu Apr 01 2021 00:00:00 GMT+0200"), new Date("Sat May 01 2021 00:00:00 GMT+0200")];
and an Date Object, which we need to search in the dateArr, for example:
var findDate = new Date("Mon Apr 05 2021 07:50:06 GMT+0200");
And all together we have this PLUS
a function to return us the nearestDate in
dateArr by findDate which can lie in the past or future
var dateArr = [new Date("Thu Apr 01 2021 00:00:00 GMT+0200"), new Date("Sat May 01 2021 00:00:00 GMT+0200")];
var findDate = new Date("Mon Apr 05 2021 07:50:06 GMT+0200");
var result = getNearestDateInDateArrByFindDate(dateArr, findDate);
console.log(result); //should print to console: Thu Apr 01 2021 00:00:00 GMT+0200
function getNearestDateInDateArrByFindDate(dateArr, findDate) {
var nearestDateInPastOrFuture;
...
return nearestDateInPastOrFuture;
}
What I tried so far without sucess ...
var dateArr = [new Date("Thu Apr 01 2021 00:00:00 GMT+0200"), new Date("Sat May 01 2021 00:00:00 GMT+0200")];
var findDate = new Date("Mon Apr 05 2021 07:50:06 GMT+0200");
function getNearestDateInDateArrByFindDate(dateArr, findDate) {
console.log(dateArr);
console.log(findDate);
var nearestFutureDates = dateArr.filter(dt => dt.getTime() >= findDate.getTime());
var nearestFutureDates = nearestFutureDates.sort((a, b) => a.getTime() - b.getTime());
var nearestPastOrFutureDate = dateArr.filter(dt => dt.getTime() >= findDate.getTime());
var nearestPastOrFutureDate = nearestPastOrFutureDate.sort((a, b) => (findDate.getTime() - a.getTime()) - (findDate.getTime() - b.getTime()));
console.log(nearestFutureDates);
console.log(nearestPastOrFutureDate);
//returns always sat May 01 2021 00:00:00 GMT+0200
}
getNearestDateInDateArrByFindDate(dateArr, findDate)
And somehow the snippet doesn't return Apr 01 but rather April 31?
We can use Array.sort() to sort by the difference in ms from each date to findDate.
NB: We can get the absolute difference in milliseconds between two dates using
Math.abs(date1 - date2);
So we'll use this to sort like so:
var dateArr = [new Date("Thu Apr 01 2021 00:00:00 GMT+0200"), new Date("Sat May 01 2021 00:00:00 GMT+0200")];
var findDate = new Date("Mon Apr 05 2021 07:50:06 GMT+0200");
var result = getNearestDateInDateArrByFindDate(dateArr, findDate);
console.log(result); //should print to console: Thu Apr 01 2021 00:00:00 GMT+0200
function getNearestDateInDateArrByFindDate(dateArr, findDate) {
const sortedByDiff = [...dateArr].sort((a,b) => {
// Sort by the absolute difference in ms between dates.
return Math.abs(a - findDate) - Math.abs(b - findDate);
})
// Return the first date (the one with the smallest difference)
return sortedByDiff[0];
}
You can filter what is the nearest date on past and the nearest on future.
Optionally you can apply .sort((a, b) => a - b) to your array of dates if are not ordered.
const dateArr = [new Date("Thu Apr 01 2021 00:00:00 GMT+0200"), new Date("Sat May 01 2021 00:00:00 GMT+0200")].sort((a, b) => a - b);
const findDate = new Date("Mon Apr 05 2021 07:50:06 GMT+0200");
const nearestPastDate = (dateArr, date) => {
const pastArr = dateArr.filter(n => n <= date);
return pastArr.length > 0 ? pastArr[pastArr.length-1]: null;
};
const nearestFutureDate = (dateArr, date) => {
const futArr = dateArr.filter(n => n >= date);
return futArr.length > 0 ? futArr[0]: null;
};
console.log(dateArr);
console.log(nearestPastDate(dateArr, findDate));
console.log(nearestFutureDate(dateArr, findDate));

How to set time of javascript date object

I have two widgets. A calendar and a Time Widget.
My calendar is a DateTime object Wed Apr 29 2020 00:00:00 GMT-0400 and my time outputs this result 1:00 AM
I would like to Combine the two to make a new DateTime object. May I ask how about do I do that?
I tried this and it didn't work at all.
startDate.setTime(vm.startTime)
You need to iterate your Object entries and get values to combine them through an array.
mergeDateTime() {
const DateTime = {
a: "Wed Apr 29 2020 00:00:00 GMT-0400",
b: "1:00 AM",
};
const mergedDateTime = [];
for (let [key, value] of Object.entries(DateTime)) {
mergedDateTime.push(value);
}
return mergedDateTime;
}
I'm not sure but I think you want this.
I hope I've been helpful
var startDate = { Date :"Wed Apr 29 2020 00:00:00 GMT-0400" };
var startTime = { Time :"1:00 AM" };
var DateTime = Object.assign(startDate, startTime);
console.log(DateTime);
// { Date: 'Wed Apr 29 2020 00:00:00 GMT-0400', Time: '1:00 AM' }
Edit:
var startDate = { Date :"Wed Apr 29 2020 00:00:00 GMT-0400" };
var startTime = { Time :"1:00 AM" };
var DateTime = { DateAndTime: startDate.Date + " " + startTime.Time};
console.log(DateTime);
//{ DateAndTime: 'Wed Apr 29 2020 00:00:00 GMT-0400 1:00 AM' }
Try with splitting you time string and setting Hours value of your date object.
var startDate = "Wed Apr 29 2020 00:00:00 GMT-0400";
var startTime = "1:00 AM";
var result = new Date(startDate);
var hour = parseInt(startTime.split(":")[0]);
var minute = parseInt(startTime.split(":")[1].split(" ")[0]);
var amPmOffset = startTime.split(" ")[1] == "AM" ? 0 : 12;
result.setHours(hour, minute + amPmOffset, 0);
console.log(result.toString());

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

How to Format JavaScript Long String Date

I am using this code to export dates of current week days
var arr =[];
var curr = new Date();
var first = curr.getDate() - curr.getDay();
for (var i = 1; i < 6; i++) {
var next = new Date(curr.getTime());
next.setDate(first+1 );
arr.push(next.toString());
}
but the output looks like Mon Nov 09 2015 01:43:57 GMT-0800 (Pacific Standard Time) in the array of
["Mon Nov 09 2015 01:43:57 GMT-0800 (Pacific Standard Time)", "Mon Nov
09 2015 01:43:57 GMT-0800 (Pacific Standard Time)", "Mon Nov 09 2015
01:43:57 GMT-0800 (Pacific Standard Time)", "Mon Nov 09 2015 01:43:57
GMT-0800 (Pacific Standard Time)", "Mon Nov 09 2015 01:43:57 GMT-0800
(Pacific Standard Time)"]
Can you please let me know how I can format the date() to get only
Mon Nov 09 2015 and remove 01:43:57 GMT-0800 (Pacific Standard Time)?
Thanks
You can use the toDateString() method on the Date object.
arr.push(next.toDateString());
Its simple. I am using your code. just use toDateString() method of Date()
var arr =[];
var curr = new Date();
var first = curr.getDate() - curr.getDay();
for (var i = 1; i < 6; i++) {
var next = new Date(curr.getTime());
next.setDate(first+1 );
arr.push(next.toDateString());
}
document.write(new Date().toDateString());

Getting first and last day of three consecutive month in 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

Categories

Resources