one-day-js
v0.1.5
Published
A javascript common use date utilities and compatible with typescript.
Downloads
29
Maintainers
Readme
one-day
A javascript common use date utilities and compatible with typescript.
Install
npm install one-day --save
yarn add one-day
Usage
calDate
calcDate(date: Date, calcType: 'add' | 'subtract', unitType: OpUnitType, value: number): Dayjs
Add or Subtract date by unit type.
Example
console.log(calcDate(new Date(date), 'add', 'day', 10).format('DD-MMM-YYYY'));
//expected output: "15-Apr-2020"
dayToTime
dayToTime(day: number, type?: 'ms' | 's' | 'm' | 'h'): number
Convert from day(s) to millisecond, second, minute and hour.
Example
console.log(dayToTime(1, 'h'));
//expected output: 24
fromNow
fromNow(date?: ConfigType): string
Returns the string of relative time from now
Example
console.log(fromNow(new Date()));
// expected output: "a second ago"
getFirstDayOfMonth
getFirstDayOfMonth(date: Date, type: 'Previous' | 'Current' | 'Next'): Dayjs
Get the first day of the month.
Example
const date = '2020-04-05';
const format = 'DD-MMM-YYYY';
console.log(getFirstDayOfMonth(new Date(date), 'Previous').format(format));
// expected output: '01-Mar-2020'
console.log(getFirstDayOfMonth(new Date(date), 'Current').format(format));
// expected output: '01-Apr-2020'
// Next month
console.log(getFirstDayOfMonth(new Date(date), 'Next').format(format));
// expected output: '01-May-2020'
getLastDayOfMonth
getLastDayOfMonth(date: Date, type: 'Previous' | 'Current' | 'Next'): Dayjs
Get the last day of the month.
Example
const date = '2020-04-05';
const format = 'DD-MMM-YYYY';
console.log(getLastDayOfMonth(new Date(date), 'Previous').format(format));
// expected output: '31-Mar-2020'
console.log(getLastDayOfMonth(new Date(date), 'Current').format(format));
// expected output: '30-Apr-2020'
console.log(getLastDayOfMonth(new Date(date), 'Next').format(format));
// expected output: '31-May-2020'
previousMonth
previousMonth(format?: string);
Get the first and last day of previous month.
Example
// '2020-04-05';
console.log(previousMonth());
// expected output: { from: '01-Mar-2020', to: '31-Mar-2020' }
previousWeek
previousWeek(format?: string);
Get the first and last day of previous week.
Example
// '2020-05-19';
console.log(previousWeek());
// expected output: { from: '10-May-2020', to: '16-May-2020' }
quarter
previousWeek(format?: string);
Gets the quarter of the year.
Example
// '2020-05-19';
// Get Current Quarter
quarter.current();
// expected output: { from: '01-Apr-2020', to: '30-Jun-2020' }
// Get Previous Quarter
quarter.previous();
// expected output: { from: '01-Jan-2020', to: '31-Mar-2020' }
// Get Specific Quarter
quarter.quarter(1);
// expected output: { from: '01-Jan-2020', to: '31-Mar-2020' }
smartTZ
smartTZ(utc: string, options?: smartTZOptions): string | Dayjs
Convert UTC date to specific time zone.
Example
const format = 'DD-MM-YYYY HH:mm:ss';
const UTCString = '2019-07-25T16:00:00.48Z';
smartTZ(UTCString, { timezone: 'Asia/Jakarta', format: format });
// expected output: 25-07-2019 23:00:00
thisMonth
thisMonth((format?: string));
Get the first and last day of current month.
Example
// 2020-05-19
thisMonth();
// expected output: { from: '01-May-2020', to: '31-May-2020' }
thisWeek
thisWeek((format?: string));
Get the first and last day of current week.
Example
// 2020-05-19
thisWeek();
// expected output: { from: '17-May-2020', to: '23-May-2020' }
thisYear
thisYear((format?: string));
Get the first and last day of current year.
Example
// 2020-05-19
thisYear();
// expected output: { from: '01-Jan-2020', to: '31-Dec-2020' }
today
today((format?: string));
Get today date.
Example
// 2020-05-19
today();
// expected output: { from: '19-May-2020', to: '19-May-2020' }
yesterday
today((format?: string));
Get yesterday date.
Example
// 2020-05-19
yesterday();
// expected output: { from: '18-May-2020', to: '18-May-2020' }