0

I recently discovered a form of using a conditional as:

function leftPad(res6) {return (res6 < 10)?'0'+ res6:res6;}
res6 =leftPad(res6);

This is fine inside a function statement. I don't know how to work with this sort of statement on it's own (without a function statement of if there is an appropriate function statement for this string).

Example: I want to have output that compares a date with todays value, if It is after today then use "since", otherwise, use "until". Once I understand how to write this statement I can compare the date values and set a variable to the proper result, to go in my output string.

Thanks

levelbest
  • 29
  • 2
  • 1
    your two statements could be written as one ... `res6 = (res6 < 10)?'0'+ res6:res6;` ... does this answer your question? – Jaromanda X Sep 26 '15 at 16:01
  • ^^ What @JaromandaX said. It doesn't even need the `()`: `res6 = res6 < 10 ? '0' + res6 : res6;` – T.J. Crowder Sep 26 '15 at 16:03
  • That will help clean up the code in my previous question. Thanks for that suggestion. But this current question still remains. – levelbest Sep 26 '15 at 16:22
  • That type of statement is called a ternary. – chRyNaN Sep 26 '15 at 16:23
  • 1
    Just to make it clear in basic syntactic terms, `?:` is an *operator* and when written with valid operands the whole thing is an *expression*, not a *statement*. So it goes wherever you can write an expression, which is nearly anywhere in JavaScript. – Touffy Sep 26 '15 at 16:34
  • This is how I want It to work. My attempt at comparing the two values is not working. var one_day=1000*60*60*24; // milliseconds var d0 = new Date() var d2 = new Date("October 1, 2015 12:00:00"); // “Some Event” var diff2 = Math.abs(d0-d2); // difference in milliseconds Var now_then2 {return (d0 < d2)?”until”:”since”;} // Not working var res2 = Math.round(diff2/one_day); function leftPad(res2) {return (res2 < 10)?'0'+ res2:res2;} res2 =leftPad(res2); res2 + " Days “ + now_then2 + “ 10/01/2015 Some Event" + "\n" – levelbest Sep 26 '15 at 16:45

1 Answers1

0

What you're descibing there is the Conditional (ternary) Operator. It does not need to be in a function, and your code could be written in one line, as T.J. Crowder said.

res6 = res6 < 10 ? '0' + res6 : res6;

For the second part, and answering your comments, you want to use the Date object.

var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date();
var secondDate = new Date("October 1, 2015 12:00:00");

var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
var since_until = d2 > d0 ? 'since' : 'until'; 

var sentence = "- " + diffDays + " Days " + since_until + " 10/01/2015 - Oct Pension" + "\n";

With reference to this question: How to calculate the number of days between two dates using JavaScript?

Community
  • 1
  • 1
enigma
  • 3,476
  • 2
  • 17
  • 30
  • The variable is printing as the variable text name with quotes. Not sure why but obviously I have screwed up the quotation marks somehow? Thanks. var d0 = new Date() var d2 = new Date("October 1, 2015 12:00:00"); // "Some Event" var now_then2 = d2 > d0 ? 'since' : 'until'; var diff2 = Math.abs(d0-d2); // difference in milliseconds var res2 = Math.round(diff2/one_day); res2 = res2 < 10 ? '0' + res2 : res2; "- " + res2 + " Days “ + now_then2 + “ 10/01/2015 - Oct Pension" +"\n"+ // Output is: - 05 Days “ + now_then2 + “ 10/01/2015 - Some Event – levelbest Sep 26 '15 at 17:29
  • I appreciate your marking up an example. The problem I am having is that I am getting different results from the output. Putting in "- " + res6 + " Days “ + word6 + “ 11/01/2015 - Some Data and word6, proves that my variable word6 has correct data in It. But It prints out as a string with the variable and the quotation marks and the variable is It’s name and not It’s value. - 36 Days “ + word6 + “ 11/01/2015 - Some Data – levelbest Sep 26 '15 at 22:56
  • @levelbest you're using weird quotation marks, and that's probably the root cause of the problem. Write code in a suitable editor, or even notepad, but not a word processor such as Word. If you want to even copy and paste them, the only two types of quotation mark you should use are " and '. Some of yours are curvy ones. – enigma Sep 26 '15 at 23:05
  • Good call, thanks very much! I had to work It a few times in BBEdit, zap the gremlins, straighten the quotes, but I got there. BTW, as aside perhaps, I am doing this in TextExpander as It allows Javascript now. – levelbest Sep 27 '15 at 02:24
  • @levelbest Great, glad it works. If this answers your question, consider accepting it as an answer by clicking the little tick next to the voting buttons. – enigma Sep 27 '15 at 09:11