Questions tagged [date-fns]

date-fns is a JavaScript date/time library that provides a comprehensive, simple and consistent toolset for manipulating JavaScript dates in a browser and Node.js.

date-fns provides a comprehensive, simple and consistent toolset for manipulating JavaScript dates in a browser and Node.js.

date-fns supports both Flow and TypeScript and it works well with module bundlers such as webpack, Browserify, or Rollup and also supports tree-shaking.

Features:

date-fns helps:

  • showing JavaScript dates in a given format
  • checking if a date isAfter or isBefore another
  • adding or removing units of time (addHours, subHours, addMinutes, subMinutes etc)

and it offers other functionalities listed in the documentation.

Example:

Represent a given date in middle-endian format:

var result = format(
  new Date(2014, 1, 11),
  'MM/DD/YYYY'
)
//=> '02/11/2014'

Resources:

496 questions
17
votes
2 answers

moment vs date-fns locale date formats

I'm evaluating DateFns and Moment in the context of our app, and found what appears to be an important omission in DateFns. In Moment, locale support allows you to format locale-correct representations of a date or time. For instance, the date…
Mud
  • 28,277
  • 11
  • 59
  • 92
16
votes
1 answer

How to skip some characters from formatting in date-fns

In the folowing example I want to say date-fns do not format at (skip these characters): format(DATE, 'cccc, MMMM d at h:mm a') Output is: Thursday, August 8 AM492296400 12:30 AM Expected is: Thursday, August 8 at 12:30 AM
Iman Mahmoudinasab
  • 6,861
  • 4
  • 44
  • 68
16
votes
1 answer

moment utc, does date-fns have something similar?

I'm currently refactoring a large codebase that are using moment for dates. There are lots of places where the date in gotten like: moment.utc() I cant seem to find something like it in date-fns? Does anyone know how I get the same functionality in…
user431619
16
votes
1 answer

Get the difference between to dates in minutes with Date-FNS - NaN Error

I'm using the Date-FNS library to get the difference between to dates in minutes. Why does minutesDifference return NaN? My goal is to get number of minutes between the given dates. Here is the link to the Date-FNS doc. getDateTime: function () { …
Tom
  • 5,588
  • 20
  • 77
  • 129
15
votes
3 answers

Getting nominative month name in date-fns

How can I get the name of the month in nominative format? import { format } from 'date-fns'; import { ru } from 'date-fns/locale'; format(new Date(), 'MMMM', { locale: ru }); // июня How can I get the name like юинь instead of июня?
Roman Mahotskyi
  • 4,576
  • 5
  • 35
  • 68
14
votes
2 answers

How to make react-datepicker start the days of the week on Monday?

I'm using react-datepicker along with date-fns to display a date picker with Hungarian locale in my Web app, but I couldn't find a way for it to display the dates of the week starting with Monday instead of Sunday as per Hungarian conventions. The…
Szelmat
  • 189
  • 1
  • 3
  • 10
12
votes
3 answers

Proper way to parse a date as UTC using date-fns

I have a log file with some timestamps 2020-12-03 08:30:00 2020-12-03 08:40:00 ... I know from the log provider's documentation that the timestamps are written in UTC (although not using ISO format) Now I want to parse them with date-fns : const…
Yann Pellegrini
  • 793
  • 3
  • 7
  • 19
12
votes
3 answers

Countdown in date-fns with years, months, days, hours & minutes

I'm using DateFNS and I need to generate a countdown with it. distanceInWordsToNow only outputs in about 3 years but I need the exact time like 3 Years, 11 Months, 20 Days, 3 Hours, 2 Minutes. How to archive that with DateFNS? Here is a CodePen…
Tom
  • 5,588
  • 20
  • 77
  • 129
11
votes
2 answers

How do I get "time ago" with a date provided by mysql?

I searched around the documentions of some popular time parsing and formatting libraries like Day.js and date-fns, but I didn't really get it. My Node.js backend queries the datetime of creation of a record in my MySQL database and sends it to the…
Mr.Joony
  • 441
  • 2
  • 6
  • 8
11
votes
3 answers

Convert new Date() to weekday string eg 'Thursday' using date-fns

I'm using date-fns I would like to convert new Date(2020,1,10) to 'Thursday'. I'm trying to use dateFns.format(new Date(2020,1,10),'dddd') but it returns 0001? Thanks in advance
James
  • 1,355
  • 3
  • 14
  • 38
10
votes
2 answers

timezone conversion using date-fns

I’m trying to work with date-fns-tz in my react-based webpage and couldn’t make the following use-case to work. I have a date input in a form that should be submitted to the backend that stores the data in local timezone. A user in GMT+2 timezone…
Or Dotan
  • 103
  • 1
  • 1
  • 5
10
votes
2 answers

How to customize date-fns's formatRelative?

In moment, with calendar, I can customize how to show the time like below moment(dateTime).calendar(null, { sameDay: '[Today]', nextDay: '[Tomorrow]', nextWeek: 'dddd', lastDay: '[Yesterday]', lastWeek: '[Last] dddd', …
Hongbo Miao
  • 45,290
  • 60
  • 174
  • 267
9
votes
1 answer

Failing to "dynamically" import date-fns/locale libs - TypeScript giving an attempted import error

I have an app that receives a list of supported locales from the backend as a following response: {locales: [{code: 'enUS'}, {code: 'deDE'}, {code: 'arAR'}]} I want to use date-fn library for handling date formatting but I have to import the whole…
Whichmann
  • 153
  • 1
  • 7
9
votes
4 answers

Format time interval in seconds as X hour(s) Y minute(s) Z second(s)

Is it possible to format seconds in a countdown style? For example, if I have var seconds = 3662, how can I show: 1 hour 1 minute 2 seconds. I tried using formatDistanceStrict(3662) but this is only printing the hour: 1 hour. Is there a built-in…
Ionel Lupu
  • 2,695
  • 6
  • 29
  • 53
9
votes
2 answers

How to find the nearest day of the week with date-fns

I want to be able to find out the nearest Day of the week in the past based on the current date with date-fns. Say I need to find nearest Friday, Wednesday, Thursday etc in the past based on the current date. I looked in to the documentation and can…
Sergino
  • 10,128
  • 30
  • 98
  • 159
1
2
3
33 34