2

I would like to convert a string value that I fetch from my database and convert it to a date variable in the view using javascript

I have tried out the following code;

var date = moment({!! $cheque->cheque_date !!}).format('YYYY-MM-DD');

I am getting the following result when I console log the date variable. 1970-01-01

5
  • what is the value of $cheque->cheque_date Commented Apr 5, 2019 at 13:07
  • @Ali the value is 2019-04-10. However, when I try to console log it I get a NaN error Commented Apr 5, 2019 at 13:09
  • 1
    You need javascript quotes around that php string output and probably a format argument for moment(string, format) Commented Apr 5, 2019 at 13:10
  • 1
    var date = moment('{!! $cheque->cheque_date !!}').format('YYYY-MM-DD'); Commented Apr 5, 2019 at 13:11
  • @charlietfl and Haurn Yilmaz thank you for your answers Commented Apr 5, 2019 at 13:15

1 Answer 1

3

you need to add " quotations

checkout this

var date = moment("{{ $cheque->cheque_date }}").format('YYYY-MM-DD');

this will converted to

var date = moment("2019-04-10").format('YYYY-MM-DD');

while your code will be converted to

var date = moment(2019-04-10).format('YYYY-MM-DD'); // no quotations 
Sign up to request clarification or add additional context in comments.

1 Comment

happy to help :)

Your Answer

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.