0

I'm trying to write code that will have a Date be converted to text such as "8:00 AM" not sure where I'm tripping up. I'm using http://labs.codecademy.com/#:workspace to test this code.

var today = new Date(); 

   function calculateMeridian(num){
    if (num < 12) {
        return "AM";
    } else if (num < 24) {
        return "PM";
    } else if (num > 24)
        console.log("calculateMeridian error: num too large, num not in hours");
        return "error";
}

 function convertMStoText(date) { //input as milliseconds, can't seem to input as actual date
    var x = new Date(date); 
    var h = x.getHours; //change to let
    var m = x.getMinutes;
    var meridian = calculateMeridian(h);
    console.log(h + ":" + m + " " + meridian);
}


convertMStoText(today);

Output:

function getHours() { [native code] }:function getMinutes() { [native code] } error

Output Image:

Mathews Sunny
  • 1,796
  • 7
  • 21
  • 31
Logrui
  • 9
  • 1
  • You need to call the methods. – jhpratt Dec 24 '17 at 17:33
  • Could really use some pointers, new at development. – Logrui Dec 24 '17 at 17:34
  • So x.Date.getHours? – Logrui Dec 24 '17 at 17:34
  • See Dave's answer. – jhpratt Dec 24 '17 at 17:35
  • What is the value of *date* in `new Date(date)`? Using the built-in parser is strongly recommended against. Also, using am/pm you should mod the hours value and pad single digit minutes with a leading zero. Probably a duplicate of [*Where can I find documentation on formatting a date in JavaScript?*](https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – RobG Dec 25 '17 at 08:09

3 Answers3

2
var h = x.getHours;
var m = x.getMinutes;

needs to be

var h = x.getHours();
var m = x.getMinutes();
dave
  • 62,300
  • 5
  • 72
  • 93
  • In Javascript, functions (or methods, pick your term) are values like any other. `x.getHours` is a function, and you can assign it to `h`, but that doesn't _call_ it. To call a function, you need to put parentheses after it, which is what you're missing here. (You could, in fact, try something like`var h = x.getHours; h();` but the result wouldn't be the same because the version of `getHours` pointed to by `h` doesn't remember that it goes with `x`; you could remind it by calling `h.apply(x)`.) – Mark Reed Dec 24 '17 at 17:44
2

The computer outputs exactly what you asked it to.

Look at this line:

var h = x.getHours;

If assigns a function to h. What you intended was a function call, assigning the result returned by the function:

var h = x.getHours();

It reads: "call method getHours on on object x passing no parameters". This is what the empty parens are for.

Same with minutes.

9000
  • 39,899
  • 9
  • 66
  • 104
0

Update your function:

function convertMStoText(date) { //input as milliseconds, can't seem to input as actual date
    var x = new Date(date); 
    var h = x.getHours(); //change to let
    var m = x.getMinutes();
    var meridian = calculateMeridian(h);
    console.log(h + ":" + m + " " + meridian);
}

getHours() and getMinutes() are functions.