Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
Date object passed as prop becomes [object Object] instead of [object Date]

A component in my project is calling a function from another file and passing it an object with three properties: stringProp, numProp, and dateProp (which is a Date object the current component receives as a prop from an input component)

const result = MyAPI.calc({stringProp: {stringProp}, numProp: {numProp}, dateProp: {dateProp}})

console.log(`{stringProp: ${stringProp}, numProp: ${numProp}, dateProp: ${dateProp} ${Object.prototype.toString.call(dateProp)}}`)
// >>> {stringProp: something, numProp: 123, dateProp: Fri Jan 19 2024 [object Date]}

When the calc function is called:

export const calc = (props) =>
{
    const stringProp = props.stringProp || ''
    const numProp = props.numProp || 0

    console.log(`props.dateProp: ${props.dateProp} ${Object.prototype.toString.call(dateProp)}`)
    // >>> props.dateProp: [object Object] [object Object]
    ...
}

numProp and stringProp are coming through as expected, but dateProp is not. These three props were already passed as parameters from one component to another, and came through as expected.

In the original input component, dateProp is saved to state with the useState hook.

I've seen a few similar questions, but not many have been answered, or the solution was to fix a typo which I don't think is the case now.

Update: per @KyleRifqi's answer below, calling JSON.stringify(props.dateProp, null, '\t') will return a value like "2024-01-19T00:00:00.000Z" which shows typeof string. How would I convert this into a date? new Date() does not work and returns Invalid Date. There are a lot of older answers using match and/or a reviver function to convert it, but is there a more modern robust approach?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

When an object is converted to a string, it is converted as "[object Object]". If you want to console log an object use the JSON.stringify() function.

Example:

console.log(`props.dateProp: ${JSON.stringify(props.dateProp, null, '\t')}`)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!