({ value, type, now }: FormatDateProps)
| 308 | } |
| 309 | |
| 310 | export const formatDate = ({ value, type, now }: FormatDateProps): string => { |
| 311 | const date = new Date(value); |
| 312 | |
| 313 | if (!isValidDate(date)) { |
| 314 | return ''; |
| 315 | } |
| 316 | |
| 317 | const nowDate = now ? new Date(now) : undefined; |
| 318 | if (nowDate && !isValidDate(nowDate)) { |
| 319 | return ''; |
| 320 | } |
| 321 | |
| 322 | if (type === TimeFormatType.Elapsed) { |
| 323 | return publishTimeRelativeShort(date, nowDate); |
| 324 | } |
| 325 | |
| 326 | if (type === TimeFormatType.Post) { |
| 327 | return postDateFormat(date); |
| 328 | } |
| 329 | |
| 330 | if (type === TimeFormatType.PostUpdated) { |
| 331 | return postUpdatedDateFormat(date); |
| 332 | } |
| 333 | |
| 334 | if (type === TimeFormatType.Comment) { |
| 335 | return publishTimeRelativeShort(date); |
| 336 | } |
| 337 | |
| 338 | if (type === TimeFormatType.ReadHistory) { |
| 339 | return getReadHistoryDateFormat(date); |
| 340 | } |
| 341 | |
| 342 | if (type === TimeFormatType.TopReaderBadge) { |
| 343 | return getTopReaderBadgeDateFormat(date); |
| 344 | } |
| 345 | |
| 346 | if (type === TimeFormatType.PlusMember) { |
| 347 | return getPlusMemberDateFormat(date); |
| 348 | } |
| 349 | |
| 350 | if (type === TimeFormatType.Transaction) { |
| 351 | const isCurrentYear = isSameYear(date, new Date()); |
| 352 | |
| 353 | return format(date, `MMM dd${isCurrentYear ? ' ' : ', yyyy '}HH:mm`); |
| 354 | } |
| 355 | |
| 356 | if (type === TimeFormatType.LastActivity) { |
| 357 | return getLastActivityDateFormat(date); |
| 358 | } |
| 359 | |
| 360 | if (type === TimeFormatType.LiveTimer) { |
| 361 | return publishTimeLiveTimer(date, nowDate); |
| 362 | } |
| 363 | |
| 364 | if (type === TimeFormatType.Experience) { |
| 365 | return publishExperienceTime(date, nowDate); |
| 366 | } |
| 367 |
no test coverage detected