Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

gettingStarted.md 2.0 KiB

3 anos atrás
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Getting Started
  2. ## Table of Contents
  3. - [Introduction](#introduction)
  4. - [Submodules](#submodules)
  5. - [Installation](#installation)
  6. ## Introduction
  7. **date-fns** provides the most comprehensive, yet simple and consistent toolset
  8. for manipulating **JavaScript dates** in **a browser** & **Node.js**.
  9. **date-fns** is like [lodash](https://lodash.com) for dates. It has
  10. [**140+ functions** for all occasions](https://date-fns.org/docs/).
  11. ```js
  12. import { format, compareAsc } from 'date-fns'
  13. format(new Date(2014, 1, 11), 'MM/dd/yyyy')
  14. //=> '02/11/2014'
  15. const dates = [new Date(1995, 6, 2), new Date(1987, 1, 11), new Date(1989, 6, 10)]
  16. dates.sort(compareAsc)
  17. //=> [
  18. // Wed Feb 11 1987 00:00:00,
  19. // Mon Jul 10 1989 00:00:00,
  20. // Sun Jul 02 1995 00:00:00
  21. // ]
  22. ```
  23. ## Submodules
  24. **date-fns** includes some optional features as submodules in the npm package.
  25. Here is the list of them, in order of nesting:
  26. - FP — functional programming-friendly variations of the functions. See [FP Guide](https://date-fns.org/docs/FP-Guide);
  27. - UTC (in development) — variations of the functions which calculate dates in UTC±00:00 timezone.
  28. The later submodules are also included inside the former if you want to use multiple features from the list.
  29. To use submodule features, [install the npm package](#npm) and then import a function from a submodule:
  30. ```js
  31. // The main submodule:
  32. import addDays from 'date-fns/addDays'
  33. // FP variation:
  34. import addDays from 'date-fns/fp/addDays'
  35. // UTC variation:
  36. import addDays from 'date-fns/utc/addDays'
  37. // Both FP and UTC:
  38. import addDays from 'date-fns/fp/utc/addDays'
  39. // With tree-shaking enabled:
  40. import { addDays, format } from 'date-fns/fp'
  41. ```
  42. ## Installation
  43. The library is available as an [npm package](https://www.npmjs.com/package/date-fns).
  44. To install the package, run:
  45. ```bash
  46. npm install date-fns --save
  47. # or
  48. yarn add date-fns
  49. ```
  50. Start using:
  51. ```js
  52. import { formatDistance, subDays } from 'date-fns'
  53. formatDistance(subDays(new Date(), 3), new Date())
  54. //=> "3 days ago"
  55. ```