最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Format date with date-fns - Stack Overflow

programmeradmin14浏览0评论

Am trying to format date with such an example 2021-09-20T12:12:36.166584+02:00 with date-fns library but it doesn't work, what's the right approach ?

Below is my code :

import { format } from 'date-fns'
format(date, 'DD.MM.YYYY')

Am trying to format date with such an example 2021-09-20T12:12:36.166584+02:00 with date-fns library but it doesn't work, what's the right approach ?

Below is my code :

import { format } from 'date-fns'
format(date, 'DD.MM.YYYY')
Share Improve this question edited Sep 21, 2021 at 9:01 Will Jenkins 9,7871 gold badge29 silver badges48 bronze badges asked Sep 21, 2021 at 8:48 Lutaaya Huzaifah IdrisLutaaya Huzaifah Idris 3,9909 gold badges43 silver badges86 bronze badges 2
  • 1 define 'doesn't work' please – Will Jenkins Commented Sep 21, 2021 at 9:01
  • Try to use lower case like yyyy-mm-dd – Amir Meimari Commented Sep 21, 2021 at 9:07
Add a comment  | 

2 Answers 2

Reset to default 11

You have to parse your ISO string before you can format it, and use lowercase dd and yyyy:

import { format, parseISO } from "date-fns";

const dateFormatted = format(parseISO(date), "dd.MM.yyyy");

We definitely need more information on what the problem is.

But I guess you want to have a day of the month to be formatted so you should put dd instead of DD.

DD = 01, 02, ..., 365, 366

dd = 01, 02, ..., 31

And yyyy instead of YYYY.

And your input param date should be a type of Date:

format(new Date('2021-09-20T12:12:36.166584+02:00'), 'dd.MM.yyyy')

Everything can be found in documentation (https://date-fns.org/v2.24.0/docs/format)

发布评论

评论列表(0)

  1. 暂无评论