Grapes homepage1.63.0
  • Guide
  • Tokens
  • Components
    Grapes on GithubGrapes on Figma
    Interaction
    • Button
    • IconButton
    • FloatingActionBar
    • Link
    Icons
    • Icon
    • HighlightIcon
    Form
    • AmountInput
    • Autocomplete
    • AutocompleteMultiple
    • AutocompletePlace
    • CheckboxBox
    • CheckboxField
    • DatePicker
    • FormField
    • Input
    • OptionGroup
    • PasswordInput
    • PhoneInput
    • RadioBox
    • RadioField
    • RadioGroup
    • Select
    • SwitchField
    • TextArea
    • TextInput
    • Upload
    • UploadButton
    Feedback
    • Badge
    • Banner
    • Callout
    • EmptyState
    • Modal
    • ModalSlideshow
    • DeprecatedModalSlideshow
    • PageModal
    • Skeleton
    • Tag
    • Toast
    • Tooltip
    Data display
    • Accordion
    • Avatar
    • Box
    • Calendar
    • CalendarRange
    • CollapsibleList
    • FileCard
    • InfoTip
    • ListBox
    • ListView
    • Panel
    • SidePanel
    • DeprecatedPreview
    • Table
    • Timeline
    • useDateFormatter
    Navigation
    • DropdownItem
    • DropdownMenu
    • Navigation
    • NavigationItem
    • Popover
    • Tabs

    useDateFormatter

    useDateFormatter is a hook providing localized date for the current locale. It is built around the native Intl.DateTimeFormat API with predefined date formats but can also be used using the same API.

    See the MDN docs for full details on how to use the API.

    useDateFormatter is built on top of the function dateFormatter. This function can also be used to format a date.

    Format Output

    SHORTMEDIUMLONG_WITH_TIMECUSTOM
    en-US4/27/26Apr 27, 2026April 27, 2026 at 8:46 AM04/27
    en-UK27/04/202627 Apr 202627 April 2026 at 08:4627/04
    fr-FR27/04/202627 avr. 202627 avril 2026 à 08:4627/04
    it-IT27/04/2627 apr 202627 aprile 2026 alle ore 08:4627/04
    de-DE27.04.2627.04.202627. April 2026 um 08:4627.04.
    es-ES27/4/2627 abr 202627 de abril de 2026 a las 8:4627/4

    Example

    import { useDateFormatter, DATE_FORMAT } from "@spendesk/grapes";
    
    export default function ProductPage() {
      const dateFormatter = useDateFormatter();
    
      return <p>{dateFormatter(new Date(), DATE_FORMAT.MEDIUM)}</p>;
    }
    

    Example with Intl.DateTimeFormat options

    import { useDateFormatter, DATE_FORMAT } from "@spendesk/grapes";
    
    export default function ProductPage() {
      const dateFormatter = useDateFormatter();
    
      return (
        <p>
          {dateFormatter(new Date(), DATE_FORMAT.CUSTOM, {
            day: "2-digit",
            month: "2-digit",
          })}
        </p>
      );
    }
    

    Example using only dateFormatter

    import { dateFormatter } from "@spendesk/grapes";
    
    export function localeFormat(locale) {
      const formattedDate = dateFormatter(locale, new Date(), DATE_FORMAT.MEDIUM);
      return formattedDate;
    }