Grapes homepage1.57.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-US10/9/25Oct 9, 2025October 9, 2025 at 10:20 AM10/09
    en-UK09/10/20259 Oct 20259 October 2025 at 10:2009/10
    fr-FR09/10/20259 oct. 20259 octobre 2025 à 10:2009/10
    it-IT09/10/259 ott 20259 ottobre 2025 alle ore 10:2009/10
    de-DE09.10.2509.10.20259. Oktober 2025 um 10:2009.10.
    es-ES9/10/259 oct 20259 de octubre de 2025, 10:209/10

    Example

    import { useDateFormatter, DATE_FORMAT } from "@dev-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 "@dev-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 "@dev-spendesk/grapes";
    
    export function localeFormat(locale) {
      const formattedDate = dateFormatter(locale, new Date(), DATE_FORMAT.MEDIUM);
      return formattedDate;
    }