0
var timeNow = new Date();
var hours = time.getHours() > 12 ? time.getHours() - 12 : time.getHours();
hours = hours < 10 ? "0" + hours : hours;

function functionHours(){
    if(hours > 12) {
        return (time.getHours()-12)
    } else {
        if(hours < 10 && hours > 12) {
            return ("0" + hours)
        } else {
            return (hours)
        } else {
            return (time.getHours())
        }
    }
}

I would to convert the ternary operations into if else statements, I've stored the statements on the functions unfortunately it returns an error unexpected token else. What I would like to do is to add 0 to the hours if the hour is 1-9 (E.g. 09, 08, 05, etc...) What seems to be the problem?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Ricky
  • 29
  • 4
  • 4
    You cannot have two `else` clauses for one `if` clause. – 31piy Mar 23 '18 at 06:17
  • `hours = hours > 12 ? hours : ("0" + hours).slice(-2)` – gurvinder372 Mar 23 '18 at 06:20
  • Re: the 2 else-clauses in an if-block. Not sure if this is caused by copy-pase, but my recommendation would be to always format your code with tabs(spaces!) correctly and have each logical if/else expression starting on the same line which makes spotting errors like these easy. – ReGdYN Mar 23 '18 at 06:22
  • @gurvinder372 I've already figured that out...what I'm looking at is to transform the ternary operation applied on hours as function using if statements not another ternary operation. – Ricky Mar 23 '18 at 06:29
  • why is hour sometimes a number (for values smaller than `10`) or a string? with the 12 hour scheme, you loose the meridian. isuggest to use a function which converts a given 24 h style to a meridian style in one function, to prevent, like in the above code to call a function over and over. – Nina Scholz Mar 23 '18 at 06:53
  • Where do you set the variable `time`? Should that be `timeNow`? And the function needs to take parameters `hours` and `time`. – Barmar Mar 23 '18 at 07:07
  • Why don't you add a leading 0 when `hours > 12`? – Barmar Mar 23 '18 at 07:08

3 Answers3

1

Simply make it

var hours = time.getHours();
if (hours < 12)
{
   hours = ("0" + hours).slice(-2);
}

No need to check for scenario where hours is greater than 12 at all.

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

Your logic is off, the following will never be true, because hours can't simultaneously be less than 10 and greater than 12 at the same time:

hours < 10 && hours > 12

Try this version, which is a close conversion of your original ternary expression:

function functionGetHours12(hours) {
    if (hours > 12) {
        hours = hours - 12
    }

    if (hours < 10) {
        return ("0" + hours)
    }
    else {
        return hours;
    }
}

console.log("hour 1: " + functionGetHours12(1));
console.log("hour 9: " + functionGetHours12(9));
console.log("hour 10: " + functionGetHours12(10));
console.log("hour 12: " + functionGetHours12(12));
console.log("hour 15: " + functionGetHours12(15));
console.log("hour 23: " + functionGetHours12(23));
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • it doesn't add a zero...I've already arrived on the same output (E.g. 2PM, 3PM). But what I wanted to do is with this format (E.g. 02:12:33 PM)...I already figured out the minutes, the seconds and the AM/PM...what my problem is the hours...Well the zero is actually needed at some format like digital watches right? – Ricky Mar 23 '18 at 06:28
  • @Ricky Then either you worded your question badly, or I misread it. From what I saw you want to get a two digit hour from 24 hour time. There are probably better ways to format timestamps in JS than this. – Tim Biegeleisen Mar 23 '18 at 06:29
  • @Tim...well I've already finish converting the seconds and the minutes, the only problem now is the hour...So I guess can I conclude that it is impossible to convert some ternary operations just like this one to functions with if statements? – Ricky Mar 23 '18 at 06:36
  • I can't really relate; AFAIK I answered the question. – Tim Biegeleisen Mar 23 '18 at 06:37
0

Please check the converted ternary operator to if-else and function:

function functionHours(){
  var time = new Date();
  var hours = time.getHours() > 12 ? time.getHours() - 12 : time.getHours();
  if (time.getHours() > 12) {
    hours = time.getHours() - 12;
  }
  else {
    hours = time.getHours();
  }

  if(hours < 10) {
          return "0" + hours;
  }
  else {
          return hours;
  }
}
console.log(functionHours());
I. Ahmed
  • 2,438
  • 1
  • 12
  • 29
  • @Ahmed...I've tried your suggestion and no luck...it doesn't add 0 to 2. What I wanted is this format (E.g. 02:30:20 pm). I've already figured the minutes and seconds, I just need to figure out how would I do the "hour" as 02, 03, 05, etc. – Ricky Mar 23 '18 at 06:34
  • Please check now. I have updated the code. – I. Ahmed Mar 23 '18 at 06:36
  • @Ricky, if(hours < 10 && hours > 12) was the culprit. I changed the condition, please check updated answer. – I. Ahmed Mar 23 '18 at 06:44
  • @Ahmed...it worked! Thank you very much for figuring it out...I've been working it out for hours already...This is exactly what I'm looking for...Converting ternary operations to a more readable form just like this - if statements inside a function...Thank you. – Ricky Mar 23 '18 at 06:46