0

I have a method:

function setToMonday( date ) {
 var day = date.getDay() || 7; 
 if( day !== 1 ) 
 date.setHours(-24 * (day - 1)); 
 return date;
}

I need to call the split method on the returned date. But split is not recognised: gg.split is not a function

var gg = setToMonday(new Date().toString());
var week1 = gg.split('T')[0];
console.log(week1);

I've seen on other Q's to use toString()But it doesn't seem to be working for me.

asked Dec 1, 2020 at 15:07
2
  • 1
    How does setToMonday not throw, when you are calling date.getDay on a string? I am confused. Commented Dec 1, 2020 at 15:09
  • You should be calling toString() on the return of setToMonday(), not as the input to setToMonday() Commented Dec 1, 2020 at 15:10

1 Answer 1

3

You're putting toString in the wrong place. You don't want to convert the date you're passing in to a string, you want to convert the date you're getting out to a string:

var gg = setToMonday(new Date());
var week1 = gg.toString().split('T')[0];
console.log(week1);
answered Dec 1, 2020 at 15:09
Sign up to request clarification or add additional context in comments.

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.