0

I'm attempting to display how much time is left, based on a mysql timestamp. For some reason the output is -1 days, 23 hours, 59 minutes, 59 seconds.

<script type="text/javascript">
function update(datetime = "2021-07-15 20:24:42") {
    timeleft = new Date(datetime);
    now = new Date();
    secs = (timeleft - now) / 1000;
    days    = Math.floor(secs / (3600 * 24));
    hours   = Math.floor((secs - (days * (3600 * 24)))/3600);
    minutes = Math.floor((secs - (days * (3600 * 24)) - (hours * 3600)) / 60);
    seconds = Math.floor(secs - (days * (3600 * 24)) - (hours * 3600) - (minutes * 60));
    if (seconds < 0) {
        days = 0;
        hours = 0;
        minutes = 0;
        seconds = 0;
    }
    return  days+' days, '+ hours+' hours, '+minutes+' minutes, '+seconds+' seconds';
}
// time is pulled from database, but plugged in manually
member = Date("06-08-21 16:06:37");
alert("Left: "+update(member));
</script>

Any info would be appreciated

2
  • What is the date format in your database? is it DD-MM-YY or YYYY-MM-DD? member suggest it's the first, and datetime suggests it's the later. Neither of these formats are suitable for javascript Date(), you'll need convert it into proper format first. Commented Jul 17, 2021 at 2:12
  • You can use the Constructor function of Date like new Date() Commented Jul 17, 2021 at 2:24

1 Answer 1

1

1) You can use new Date() to create a new instance and pass a valid date.

2) You are using dd-mm-yy format so you should make it valid because if you pass it directly then it will convert the date

member = new Date(dateString); // 2021-06-08T10:36:37.000Z

i.e Date is 08, month = 06, So you have to swap the date and month value before getting the date instance from new Date()

const dateString = "06-08-21 16:06:37".replace(
  /(\d\d)-(\d\d)-(\d\d \d\d:\d\d:\d\d)/,
  `$2-$1-$3`
);

function update(datetime = "2021-07-15 20:24:42") {
  timeleft = new Date(datetime);
  now = new Date();
  secs = (timeleft - now) / 1000;
  days = Math.floor(secs / (3600 * 24));
  hours = Math.floor((secs - days * (3600 * 24)) / 3600);
  minutes = Math.floor((secs - days * (3600 * 24) - hours * 3600) / 60);
  seconds = Math.floor(secs - days * (3600 * 24) - hours * 3600 - minutes * 60);
  if (seconds < 0) {
    days = 0;
    hours = 0;
    minutes = 0;
    seconds = 0;
  }
  return (
    days +
    " days, " +
    hours +
    " hours, " +
    minutes +
    " minutes, " +
    seconds +
    " seconds"
  );
}
// time is pulled from database, but plugged in manually
const dateString = "06-08-21 16:06:37".replace(
  /(\d\d)-(\d\d)-(\d\d \d\d:\d\d:\d\d)/,
  `$2-$1-$3`
);
member = new Date(dateString);
console.log("Left: " + update(member));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.