Home > Back-end >  Is there an another way to get date instead of getting it from user's device
Is there an another way to get date instead of getting it from user's device

Time:12-07

I want to make an application that will only open at a specific date. I use new date() to do that, but new date() get time from the user's device and the the user can open it by changing the time on its own device.

Example:

  let setdate = new Date('2021-12-10')
   let currentdate = new Date()
   console.log(setdate)
   if(setdate.getDate() == currentdate.getDate()){
    document.getElementById("result").style.display = 'revert'
   }
<div id=result style="display:none">Open now!!!</div>

If I don't do anything with the time of my device, it won't do anything. But if I change the date on my device to the specific date, then the area could open.

Could anyone suggests me a better way that could prevent this situation or is there an another way to get date instead of getting it from user's device

Thanks for any responds!!!

CodePudding user response:

Call API, and let your frontend behaves according to its response https://worldtimeapi.org/api/timezone has good API for that.

document.body.innerHTML = '<h2>Loading ...</h2>'

fetch("https://worldtimeapi.org/api/timezone/Europe/London")
  .then(r => r.json())
  .then(data => {
    let now = new Date(data.datetime);
    myLogicPerDate(now);
   
  })
  
  
  
function myLogicPerDate(date) {

    if (date.getMinutes() % 2 ) {
       document.body.innerHTML = "You cannot see anything because it's EVEN minute"
    } else {
       document.body.innerHTML = "<h1>Welcome page</h1>You are allowed to see anything because it's ODD minute"
    
    }

}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

DISCLAIMER Relying on free API is not a reliable way. So either you pay to have highest SLA from your vendor, or implement your own API & maintain its reliability.

  • Related