Why my datetime format changes while sending response to client in nestjs

Viewed 101

I m using nestjs with typeorm

Here is the code of my Entity for column created_at

  @CreateDateColumn({
    type: "datetime",
    precision: 0,
    default: () => "CURRENT_TIMESTAMP()"
  })
  created_at: Date

This code is stamping the date & time in mariadb database in a correct fprmat as expected here is the image of that

mariaDb image link

When sending response to client, this datetime format is changed from "2022-04-30 11:45:57" to "2022-04-30T06:23:59.000Z"

response image link

I m a beginner exploring nestjs to use for my upcoming project So how this problem can be solved.

Any suggestion is welcomed.

1 Answers

Presumably your data is transferred over a network (such as through an AJAX call), serialized as JSON. JSON is a text-based format. The JavaScript Data object instantiated from your MariaDB data will get serialized as a string in the ISO standard.

You will need to format it explicitly where you consume it, in order to get it in the format you want. For this, you'll probably want to instantiate a Data object from the string, and then format it further (e.g. using something like the date-fns library).

To instantiate a Date from a string:

const date = new Date(object.created_at);

or in JSX:

<div>{new Date(object.created_at).toDateString()}</div>

(Or however else you want to format it).

Related