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)
CodePudding user response:
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 onl oad="myFunction()">
<h1 id="temp"></h1>
</body>