Search code examples
javascriptreactjsamazon-dynamodbsubstringtruncate

How to Truncate the fetched Date from the DynamoDB in react


I was fetching the data from the DynamoDB and in this case the data is Date, so it also comes with the time and all,

the code to get the data,

<p>{this.sate.createdDate}</p>

Output:

2021-02-23T02:19:36.670Z

but I only need the Date should be Like this:

Expected Output:

2021-02-23

I tried using the substring method but, I didn't get that, is there any idea on this, please mention them I will try that,

Thanks for your time,


Solution

  • use the string split() method

    const date = "2021-02-23T02:19:36.670Z"
    const date_in_desired_format = time.split("T")[0]; 
    
    console.log(date_in_desired_format); // 2021-02-23
    

    an additional tip: if you want to get the date for today in the same format "ISO format"

    use this:

    cosnt today_date_in_ISO_format = new Date().toISOString().split('T')[0]
    console.log(today_date_in_ISO_format);