1

I'm trying to produce a date and time script from JS beginners, I'm sure some of you have seen this before.

I'm not sure where I've gone wrong and am struggling to problem solve it as the log just spits out 'error'.

Can anybody clear this up for me, sticking to my general way of writing the script. Explanations would be great!

var date = new Date();
var day = date.getDay();
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();
var weekdays = [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday];
var today = weekdays[day];
var amPm = amPmFunc;
function amPmFunc() {
 if(hour < 12) {
 amPm = AM;
 } else { 
 amPm = PM;
 }
}
console.log("Today is: " + today);
console.log("Current time is: " + hour + amPM + ":" + min + ":" + "sec");
asked Aug 15, 2017 at 10:47
4
  • 4
    Should Sunday etc be in quotes? Commented Aug 15, 2017 at 10:49
  • 1
    To start, all of the elements in your weekdays array need to be strings (surrounded by "). Commented Aug 15, 2017 at 10:49
  • 1
    Should AM & PM be in quotes too? Commented Aug 15, 2017 at 10:50
  • 1
    I think you should take a short course in JS to get familiar with arrays, strings, objects and formatting in JS: developer.mozilla.org/bm/docs/Learn/… Commented Aug 15, 2017 at 10:51

5 Answers 5

3

First of all, you forgot to enclose some strings with ''. i.e. Sunday should be 'Sunday'.

Also - remember - Javascript is case sensetive. So a variable called amPm will not be recognized as amPM.

Also, your function amPmFunc should return a value, rather than set a global variable's value. It's just neater this way. And when you do call it, call it with brackets: var amPm = amPmFunc();.

But other than that - Good job! ☻

See this fixed example:

var date = new Date();
var day = date.getDay();
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();
var weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var today = weekdays[day];
var amPm = amPmFunc();
function amPmFunc() {
 if(hour < 12) 
 return 'AM';
 else 
 return 'PM';
}
console.log("Today is: " + today);
console.log("Current time is: " + hour + amPm + ":" + min + ":" + "sec");

answered Aug 15, 2017 at 10:50
Sign up to request clarification or add additional context in comments.

2 Comments

Bah these syntax errors are the bane of my life >.< ...Thanks for the break down really helped.
Happy to help, and good luck with your learning. Ping me here if you have more questions. @11-
3

For why?

  1. Array declaration was wrong .Missed the quotes to formatting the string in array
  2. Javascript is case sensitive one.so don't forget case of the letter
  3. For function declare you could use () like var amPm = amPmFunc() .And add the return statement on ampm function otherwise its returning undefined .

MY suggestion :For better you good study how to declare the variable , function and string in js

var date = new Date();
var day = date.getDay();
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();
var weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var today = weekdays[day];
var amPm = amPmFunc()
function amPmFunc() {
 if (hour < 12) {
 amPm = 'AM';
 } else {
 amPm = 'PM';
 }
 return amPm
}
console.log("Today is: " + today);
console.log("Current time is: " + hour + amPm + ":" + min + ":" + "sec");

answered Aug 15, 2017 at 10:51

Comments

1
var weekdays = [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday];

They should be strings.

var weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
answered Aug 15, 2017 at 10:49

Comments

1
  1. All strings must be in "
  2. name_of_function(); instead name_of_function
  3. Functions must return somethings, yours amPmFunc() was not returning amPm

 var date = new Date();
var day = date.getDay();
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();
var weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var today = weekdays[day];
function amPmFunc() {
 if(hour < 12) {
 amPm = "AM";
 } else { 
 amPm = "PM";
 }
 return amPm;
}
var amPM = amPmFunc();
console.log("Today is: " + today);
console.log("Current time is: " + hour + amPM + ":" + min + ":" + "sec");
answered Aug 15, 2017 at 10:52

2 Comments

Perhaps single quotes are better - Saves on interpreting the string
Just added bug fix to hour function.
0

Try this

var date = new Date();
var day = date.getDay();
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();
var weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var today = weekdays[day];
var amPm = '';
 if(hour < 12) {
 amPm = 'AM';
 } else if (hour > 12){ 
 hour = hour - 12;
 amPm = 'PM';
 } else {
 amPm = 'PM'
 }
console.log("Today is: " + today);
console.log("Current time is: " + hour + amPm + ":" + min + ":" + sec);

answered Aug 15, 2017 at 11:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.