covidwatchorg / portal

Covid Watch Portal web app for diagnosis verification
Apache License 2.0
8 stars 3 forks source link

solve code expire hour time being 0 when it should be 12 #513

Closed jcolla-holla closed 4 years ago

jcolla-holla commented 4 years ago

Closes #509

Simple fix just adding an if statement if the hour time is 0, set it to 12. I think that should solve it.

jcolla-holla commented 4 years ago

I tested this for the now.getHours() being 0-23 with the code below, basically looping through 0-23 and console logging now and the return value. Feel free to run this as a test:

export const getOneHourAheadDisplayString = () => {
  // declaring new Date() here instead of passing in the code generation time stamp as they should be within 60 seconds of each other and second precision here is not necessary
  for (let index = 0; index < 24; index++) { // loop through 0-23 to mimic all possible hour values
    const now = new Date()
    now.setHours(index) // set `now` to 0-23
    console.log(now, "--now time") // see what `now` value is to compare to output in browser console
    const ahead = new Date(now.setHours(now.getHours() + 1))
    let hours = ahead.getHours()
    let amPM = 'AM'

    if (12 < hours && hours < 24) {
      hours = hours - 12
      amPM = 'PM'
    } else if (hours === 0) {
      hours = 12
    } else if (hours === 12) {
      hours = 12
      amPM = 'PM'
    }

    hours = hours.toString()
    let mins = ahead.getMinutes().toString()

    if (mins.length === 1) {
      mins = '0' + mins
    }
    const timezone = getDefaultTimezoneString()
    console.log(hours + ':' + mins + ' ' + amPM + ' ' + timezone, "--output") // this is what the function would return.  compare this to the "--now time" console log for this loop iteration
  }
  return hours + ':' + mins + ' ' + amPM + ' ' + timezone
}
ibeckermayer commented 4 years ago

See latest commit, there's a simple way to do it with builtins. Tested by pasting the following into a node console:

const getDefaultTimezoneString = () => {
  return /\((.*)\)/.exec(new Date().toString())[1]
}

const getOneHourAheadDisplayString = () => {
  for (let index = 0; index < 24; index++) {
    // loop through 0-23 to mimic all possible hour values
    let now = new Date()
    now.setHours(index)
    let ahead = new Date(now.setHours(now.getHours() + 1))
    console.log(
      `at ${new Date(now.setHours(now.getHours() - 1)).toLocaleTimeString([], {
        hour: 'numeric',
        minute: 'numeric',
      })}, getOneHourAheadDisplayString will display ` +
        `${ahead.toLocaleTimeString([], { hour: 'numeric', minute: 'numeric' })} ${getDefaultTimezoneString()}`
    )
  }
}

getOneHourAheadDisplayString()