跳到正文
DEV Community· Sherotree·· 1 小時前AI 評分57

如何要求 Agent 使用既有日期格式化器,而非在元件內另行格式化

Give the agent the existing date formatter, not a one-off toLocaleString in the component

AI 導讀

Sherotree 建議新日期顯示沿用專案既有格式化器,不要在功能元件內直接呼叫 toLocaleString 或手動切割 ISO 字串。提示詞可附上格式化器的匯出項目或既有用法;審查時搜尋 toLocaleString、toLocaleDateString、Date( 及 ISO 時間戳切片,找出繞過既有格式化器的寫法。

正文

Sherotree

The PR shows a timestamp with new Date(value).toLocaleString() or a handmade 'YYYY-MM-DD' slice. The repo already has a date formatter (formatDate, formatRelative, or an Intl wrapper) that the rest of the UI uses for locale, time zone, and relative phrases. The new string looks close in your browser and wrong in the next locale, or it disagrees with the same timestamp elsewhere on the page.

I've started treating a one-off date format in a component as a consistency bug, not as a quick display. The existing formatter is there so list, detail, and emails do not invent three styles.

Why the one-off format shows up

JavaScript examples start with toLocaleString or a short slice. The model can write that without finding formatDate. Your helper may live under lib/date or next to i18n, which is easy to miss if the prompt only says "show the updated time."

A second date library is a different problem. This one is narrower: the library (or Intl) is already wrapped, and the component bypasses the wrapper.

A simple date-format policy

  • New date and time display uses the existing formatter
  • Do not call toLocaleString or slice ISO strings in a feature component
  • A new format (date only, relative, with time zone) is a formatter change, with an example
  • Keep parsing and display in the helper, not ad-hoc in the JSX

If the value is not a date, fix the data. Do not hide a stringly timestamp with a one-off parse in the view.

What to put in the prompt

Use the existing date formatter for this timestamp.
Do not use toLocaleString or handmade ISO slices in this component.
If you need a format we do not have, add it to the formatter; do not invent it here.

Paste the formatter's exports or one existing usage. In review, search the diff for toLocaleString, toLocaleDateString, Date(, and string slices on ISO timestamps.

Reject / send back

Reject

  • A component that formats a date with toLocaleString or a handmade slice
  • A second format helper added next to the existing one
  • A view that parses and formats a timestamp inline
  • Mixed formats for the same kind of timestamp on one screen

Accept

  • A timestamp rendered with the existing formatter
  • A formatter PR that adds a documented format
  • A bugfix that replaces a one-off format with the helper

來源:DEV Community · dev.to