-2

I don't understand how this works. can someone explain it to me? Especially the variable name "prepand" which I've seen as "prepend" when I look it up. Also, what does the ? mean in (hour>12)?

var today = new Date();
var day = today.getDay();
var daylist = ["Sunday","Monday","Tuesday","Wednesday 
","Thursday","Friday","Saturday"];
       console.log("Today is : " + daylist[day] + ".");
        var hour = today.getHours();
        var minute = today.getMinutes();
        var second = today.getSeconds();
        var prepand = (hour >= 12)? " PM ":" AM ";
        hour = (hour >= 12)? hour - 12: hour;

if (hour===0 && prepand===' PM ') 
{    
   if (minute===0 && second===0)
   {  
      hour=12;
      prepand=' Noon';
   }  
   else
   {  
      hour=12;
      prepand=' PM';
   }  
}

if (hour===0 && prepand===' AM ') 
{    
   if (minute===0 && second===0)
   {  
      hour=12;
      prepand=' Midnight';
   }  
   else
   {  
      hour=12;
      prepand=' AM';
   }  
}  

console.log("Current Time : "+hour + prepand + " : " + minute + " : " + second);
F.Almeida
  • 365
  • 1
  • 10
  • 1
    The question mark is part of a ternary statement. `(conditional) ? true : false;` A variable name that is misspelled is probably due to a mistake in spelling. – zfrisch Feb 15 '18 at 18:21
  • Possible duplicate of [Question Mark in JavaScript](https://stackoverflow.com/questions/1771786/question-mark-in-javascript) – JDB still remembers Monica Feb 15 '18 at 18:21
  • `var prepand = (hour >= 12)? " PM ":" AM ";` This is a Ternary Conditional Operator. It is a really useful Conditional which in laymans means: If hour is greater than ofr equal to 12, set`prepand` to PM, if not set to AM. – NSTuttle Feb 15 '18 at 18:22
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator – NSTuttle Feb 15 '18 at 18:22

2 Answers2

0

That is a basic code which gets the current date and tell you the day and time by manipulating the date. Check the comments corresponding to the lines.

Prepand variable is used to add the AM/PM with the time

Read here in details about Date

var today = new Date(); //Creates a JavaScript Date instance that represents a single moment in time
var day = today.getDay(); // return the index of the day
var daylist = ["Sunday", "Monday", "Tuesday", "Wednesday  ","Thursday","Friday ","Saturday "];
    console.log("Today is : " + daylist[day] + ".");
    var hour = today.getHours();// getHours() method returns the hour for the specified date
    var minute = today.getMinutes();//getMinutes() method returns the minutes in the specified date
    var second = today.getSeconds();
    var prepand = (hour >= 12) ? " PM " : " AM ";
    hour = (hour >= 12) ? hour - 12 : hour; //? is a part of ternary operator which will check if hour >=12 or not. If >=  then prepand in PM otherwise AM

    if (hour === 0 && prepand === ' PM ') {
      if (minute === 0 && second === 0) {
        hour = 12;
        prepand = ' Noon';
      } else {
        hour = 12;
        prepand = ' PM';
      }
    }

    if (hour === 0 && prepand === ' AM ') {
      if (minute === 0 && second === 0) {
        hour = 12;
        prepand = ' Midnight';
      } else {
        hour = 12;
        prepand = ' AM';
      }
    }


    console.log("Current Time : " + hour + prepand + " : " + minute + " : " +
      second);
Sanchit Patiyal
  • 4,716
  • 1
  • 12
  • 31
0
var prepand = (hour >= 12)? " PM ":" AM ";

This translates to:

var prepand;
if (hour >= 12){
    prepand = "PM";
} else {
   prepand = "AM";
}

This writing is a shorthand expression called ternary. It is used to assign conditional values to variables in this case.

Laurens Mäkel
  • 753
  • 2
  • 9
  • 27