abhigyan kr
abhigyan kr

Reputation: 145

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)

Upvotes: 2

Views: 1473

Answers (2)

ArnoVoxel
ArnoVoxel

Reputation: 21

I think really quickly it's possible to use setHours :

var lastSunday = new Date(today.setDate(today.getDate()-today.getDay())); lastSunday.setHours(23,59,59,999);

And you're done

Upvotes: 1

yograj tandel
yograj tandel

Reputation: 153

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>

Upvotes: 2

Related Questions