# ivyforms/0.3.1/frontend/src/utils/utilDateFormatter.ts

The Innovative Form Builder – IvyForms, version 0.3.1. 60 lines.

- Page: https://pluginprobe.com/plugins/ivyforms/0.3.1/code/frontend/src/utils/utilDateFormatter.ts
- Raw: https://pluginprobe.com/plugins/ivyforms/0.3.1/raw/frontend/src/utils/utilDateFormatter.ts
- Modified: 2025-09-04T07:37:14+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/ivyforms/0.3.1/code/frontend/src/utils/utilDateFormatter.ts#L10-L20`.

```typescript
import { format } from 'date-fns'
import { enGB, sr } from 'date-fns/locale'

function convertWPFormat(wpFormat: string, target: 'dateFns' | 'dayjs' = 'dateFns'): string {
  const map: Record<string, { dayjs?: string; dateFns?: string; all?: string }> = {
    Y: { dayjs: 'YYYY', dateFns: 'yyyy' },
    y: { dayjs: 'YY', dateFns: 'yy' },
    F: { all: 'MMMM' },
    M: { dayjs: 'MMM', dateFns: 'MMM' },
    m: { all: 'MM' },
    n: { all: 'M' },
    d: { dayjs: 'DD', dateFns: 'dd' },
    j: { dayjs: 'D', dateFns: 'd' },
    D: { dayjs: 'ddd', dateFns: 'eee' },
    l: { dayjs: 'dddd', dateFns: 'eeee' },
  }

  return wpFormat.replace(/\b[a-zA-Z]\b/g, (token) => {
    const formats = map[token]
    return formats?.[target] || formats?.all || token
  })
}

function formatWPDate(date: string | number | Date | null | undefined | boolean): string {
  // Handle null, undefined, or boolean values
  if (!date || typeof date === 'boolean') return ''

  let jsDate: Date

  if (typeof date === 'string') {
    if (date.trim() === '') return ''
    jsDate = new Date(date)
    if (isNaN(jsDate.getTime())) return ''
  } else if (typeof date === 'number') {
    if (date === 0) return ''
    jsDate = new Date(date)
    if (isNaN(jsDate.getTime())) return ''
  } else if (date instanceof Date) {
    jsDate = date
    if (isNaN(jsDate.getTime())) return ''
  } else {
    return ''
  }

  const wpFormat = window.wpIvyDateFormat?.dateFormat || 'dd MMM yyyy'
  const locale = window.wpIvyDateFormat?.locale === 'sr_RS' ? sr : enGB
  const dateFnsFormat = convertWPFormat(wpFormat, 'dateFns')

  try {
    return format(jsDate, dateFnsFormat, { locale })
  } catch {
    return ''
  }
}

export default {
  formatWPDate,
  convertWPFormat,
}

```
