0

I get a datetime value from api in this format: 2022-11-11T08:52:01.000000Z I need to convert it to readable form with javascript.

I need this in any readable form, both time and date should be included

Nasyx Nadeem
  • 241
  • 2
  • 12

3 Answers3

2

Try it:

import moment from "moment"

const time = "2022-11-11T08:52:01.000000Z"
const formattedDate = moment(time, "DD/MM/YYYY")
console.log(formattedDate)

More information: https://momentjs.com/docs/#/parsing/string-format/

Vu Phung
  • 548
  • 3
  • 8
1

Take a look at the date-fns library:

https://date-fns.org/v2.29.3/docs/format

Easy way of converting dates to any format you desire.

warning: pseudo code

import {format} from 'date-fns'

const date = 2022-11-11T08:52:01.000000Z
const formatedDate = format(date, DD MM YY hh:mm)
Erik Dreifaldt
  • 773
  • 7
  • 13
  • Error: /data/data/com.termux/files/home/tanesco-tanzania/screens/TaskDetails.js: Unexpected token, expected "," (47:51) {format(date, DD MM YY hh:mm)} – Nasyx Nadeem Nov 11 '22 at 09:22
  • 1. Have you installed the date-fns library? 2. Did you import `format` from said library? 3. Did you read the docs regarding how to format the date? – Erik Dreifaldt Nov 11 '22 at 09:35
1

you need to install the the following package

npm install moment or yarn add moment

or you can use

npm install dayjs or yarn add dayjs

then after format your time as the following

if you choose to use moment do this

import moment from 'moment';
moment('2022-11-11T08:52:01.000000Z').format("DD/MM/YY")

and if you choose to use dayjs use

 import dayjs from "dayjs";
 dayjs('2022-11-11T08:52:01.000000Z').format("YYYY-MM-DDTHH:mm:ss") 

for more info go to their official documentation momentjs and for dayjs