PHP Classes

File: src/utils/DateHelper.ts

Recommend this page to a friend!
  Classes of Maniruzzaman Akash   WordPress React Plugin Kit   src/utils/DateHelper.ts   Download  
File: src/utils/DateHelper.ts
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: WordPress React Plugin Kit
Environment to develop new WordPress plugins
Author: By
Last change:
Date: 1 year ago
Size: 1,470 bytes
 

Contents

Class file image Download
import { format, subDays, addDays } from 'date-fns'; /** * Get default date format. * * @return string */ export function getDefaultFormat() { return 'yyyy-MM-dd'; } /** * Get Current Formatted Date. * * @param viewFormat string; Default: 'yyyy-MM-dd' * * @return {Object} Date */ export function getCurrentDate(viewFormat: string = getDefaultFormat()) { return format(new Date(), viewFormat); } /** * Get formatted date. * * @param date {Date} * @param formation * @return string */ export function getFormattedDate(date: Date, formation = getDefaultFormat()) { try { date = new Date(date); return format(date, formation); } catch (error) { // Fix for any fall-back date format. if (typeof date === 'object') { return ''; } return date; } } /** * Get Subtracted or Added Days Date. * * @param type * @param days * @param date * @param viewFormat * * @return string */ export function getSubOrAddDaysDate( type: string, days: number, date = new Date(), viewFormat: string = getDefaultFormat() ): string { date = date === null ? new Date() : date; viewFormat = viewFormat === null ? getDefaultFormat() : viewFormat; if (type === 'sub') { return format(subDays(date, days), viewFormat); } else if (type === 'add') { return format(addDays(date, days), viewFormat); } return getCurrentDate(); }