LoginSignup
0
0

More than 5 years have passed since last update.

Luxon で日付差分を日本語で

Last updated at Posted at 2019-02-21

Currently (2019-02-20) Luxon does not support relative time formatting:

Luxon is fully usable and I plan to support it indefinitely. It's also largely complete. It will certainly add relative time formatting (and an English-only fallback) when that becomes possible. Luxon will also eventually strip out its fallbacks for missing platform features. But overall I expect the core functionality to stay basically as it is, adding mostly minor tweaks and bugfixes.

I managed to implement it like this:

import { DateTime } from 'luxon'

export function translate(a: string) {
  return a
    .replace(/(?:in )?(\d+) second ago/, '$1秒前')
    .replace(/(?:in )?(\d+) seconds ago/, '$1秒前')
    .replace(/(?:in )?(\d+) minute ago/, '$1分前')
    .replace(/(?:in )?(\d+) minutes ago/, '$1分前')
    .replace(/(?:in )?(\d+) hour ago/, '$1時間前')
    .replace(/(?:in )?(\d+) hours ago/, '$1時間前')
    .replace(/(?:in )?(\d+) day ago/, '$1日前')
    .replace(/(?:in )?(\d+) days ago/, '$1日前')
    .replace(/(?:in )?(\d+) month ago/, '$1月前')
    .replace(/(?:in )?(\d+) months ago/, '$1月前')
    .replace(/(?:in )?(\d+) year ago/, '$1年前')
    .replace(/(?:in )?(\d+) years ago/, '$1年前')
}

// usage
const date = DateTime.local().plus({ days: 1 }).toRelative() // => "in 1 day"
console.log(translate(date)) // => "1日前"

Yes ugly.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0