Skip to content
On this page

Datetime

Clinux UI provides a set of useful functions to manipulate JS Date easily in most use cases, using the date-fns lib.

Overview

typescript
declare const isMoreThan2Days: (date: string) => boolean;
declare const getFormattedDate: (date: string) => string;
declare const getRawDate: (date: string) => string;
declare const getFormattedDateTime: (date: string) => string | undefined;
declare const getFormattedPrettyDateTime: (date: string) => string;
declare const getFormattedTime: (date: string) => string;
declare const getFormattedYearFromNow: () => string;
declare const isDateAfter: (date: string, after: string) => boolean;
declare const isDateBetween: (
  date: string,
  start: string,
  end: string,
) => boolean;
declare const isValidDate: (date: string) => boolean;
declare const isValidTime: (date: string) => boolean;
declare const getIsoString: (value: string) => string;
declare const getYear: () => number;
declare const isValidBornDate: (date: string) => boolean;

isMoreThan2Days

Provides a boolean wich show if the date to compare is more than 2 days compared from today.

typescript
import { isMoreThan2Days } from '@/utils';

// today is 2020-01-31

const date = '2020-02-01';

console.log(isMoreThan2Days(date));

// false
typescript
import { isMoreThan2Days } from '@/utils';

// today is 2020-01-31

const date = '2020-02-04';

console.log(isMoreThan2Days(date));

// true

getFormattedDate

Provides a DD/MM/YYYY date.

typescript
import { getFormattedDate } from '@/utils';

const date = '2020-02-01';

console.log(getFormattedDate(date));

// 01/02/2020

getRawDate

Provides a YYYY-MM-DD date.

typescript
import { getRawDate } from '@/utils';

const date = '01/02/2020';

console.log(getRawDate(date));

// 2020-02-01

getFormattedDateTime

Provides a DD/MM/YYYY [às] HH:mm date and time.

typescript
import { getFormattedDateTime } from '@/utils';

const date = '2020-02-01 04:00:00';

console.log(getFormattedDateTime(date));

// 01/02/2020 às 04:00

getFormattedPrettyDateTime

Provides a pretty date and time.

typescript
import { getFormattedPrettyDateTime } from '@/utils';

// today is 2020-01-31

const date = '2020-02-01 04:00:00';

console.log(getFormattedPrettyDateTime(date));

// Há um dia atrás

getFormattedTime

Provides a HH:mm time.

typescript
import { getFormattedTime } from '@/utils';

const hour = '04:00:00';

console.log(getFormattedTime(hour));

// 04:00

getFormattedYearFromNow

Provides the current year.

typescript
import { getFormattedYearFromNow } from '@/utils';

// Today is 2020-02-01

console.log(getFormattedYearFromNow());

// 2020

isDateAfter

Provides a boolean to show if the date and date after is true or false.

typescript
import { isDateAfter } from '@/utils';

const date = '2020-02-01';

const after = '2020-01-31';

console.log(isDateAfter(date, after));

// false
typescript
import { isDateAfter } from '@/utils';

const date = '2020-02-01';

const after = '2020-02-32';

console.log(isDateAfter(date, after));

// true

isDateBetween

Provides a boolean to show if the date is between 2 dates.

typescript
import { isDateBetween } from '@/utils';

const date = '2020-02-01';

const start = '2020-01-30';

const end = '2020-01-31';

console.log(isDateBetween(date, start, end));

// false
typescript
import { isDateBetween } from '@/utils';

const date = '2020-02-01';

const start = '2020-01-31';

const end = '2020-02-02';

console.log(isDateBetween(date, start, end));

// true

isValidDate

Provides a boolean to show if the date informed is valid.

typescript
import { isValidDate } from '@/utils';

const date = '2020-02-01';

console.log(isValidDate(date));

// true
typescript
import { isValidDate } from '@/utils';

const date = '202320-02?01';

console.log(isValidDate(date));

// false

isValidTime

Provides a boolean to show if the time informed is valid.

typescript
import { isValidTime } from '@/utils';

const time = '04:00:00';

console.log(isValidTime(time));

// true
typescript
import { isValidTime } from '@/utils';

const time = 'abs:01';

console.log(isValidTime(time));

// false

getIsoString

Provides a ISO string of date.

typescript
import { getIsoString } from '@/utils';

const time = '2020-01-31 04:00:00';

console.log(getIsoString(time));

// 2020-01-31T04:00:00Z

getYear

Provides a current year.

typescript
import { getYear } from '@/utils';

// Today is 2020-01-01

console.log(getYear());

// 2020

isValidBornDate

Provides a boolean to show if the date informed is a valid born date. The rule is the date must me greater than 31/12/1899 and lower than now.

typescript
import { isValidBornDate } from '@/utils';

const date = '2020-01-01';

console.log(isValidBornDate(date));

// true
typescript
import { isValidBornDate } from '@/utils';

const date = '1100-01-01';

console.log(isValidBornDate(date));

// false
typescript
import { isValidBornDate } from '@/utils';

const date = '2900-01-01';

console.log(isValidBornDate(date));

// false

Clinux UI - Released under the MIT License.