Giter Club home page Giter Club logo

date-utils's Introduction

npm downloads CI

Date-Utils

A collection of date-related utilities.

tl;dr

  • Install by executing npm install @wojtekmaj/date-utils or yarn add @wojtekmaj/date-utils.
  • Import by adding import * as dateUtils from '@wojtekmaj/date-utils'.
  • Do stuff with it!
    const now = new Date();
    const startOfCentury = getCenturyStart(now);

User guide

General getters

getYear()

Gets year from a given date.

Sample usage
import { getYear } from '@wojtekmaj/date-utils';

getYear(new Date(2019, 0, 1)); // 2019

getMonth()

Gets month index from a given date. For example, returns 0 for January, 1 for February and so on.

Sample usage
import { getMonth } from '@wojtekmaj/date-utils';

getMonth(new Date(2019, 0, 1)); // 0

getMonthHuman()

Gets human-readable month number from a given date. For example, returns 1 for January, 2 for February and so on.

Sample usage
import { getMonthHuman } from '@wojtekmaj/date-utils';

getMonthHuman(new Date(2019, 0, 1)); // 1

getDate()

Gets day of the month from a given date.

Sample usage
import { getDate } from '@wojtekmaj/date-utils';

getDate(new Date(2019, 0, 15)); // 15

getHours()

Gets hours from a given date or string.

Sample usage
import { getHours } from '@wojtekmaj/date-utils';

getHours(new Date(2019, 0, 15, 22, 41, 56)); // 22
getHours('22:41'); // 22
getHours('22:41:56'); // 22

getMinutes()

Gets minutes from a given date or string.

Sample usage
import { getMinutes } from '@wojtekmaj/date-utils';

getMinutes(new Date(2019, 0, 15, 22, 41, 56)); // 41
getMinutes('22:41'); // 41
getMinutes('22:41:56'); // 41

getSeconds()

Gets seconds from a given date or string.

Sample usage
import { getSeconds } from '@wojtekmaj/date-utils';

getSeconds(new Date(2019, 0, 15, 22, 41, 56)); // 56
getSeconds('22:41'); // 0
getSeconds('22:41:56'); // 56
getSeconds('22:41:56.321'); // 56

getMilliseconds()

Gets milliseconds from a given date or string.

Sample usage
import { getMilliseconds } from '@wojtekmaj/date-utils';

getMilliseconds(new Date(2019, 0, 15, 22, 41, 56, 321)); // 321
getMilliseconds('22:41'); // 0
getMilliseconds('22:41:56'); // 0
getMilliseconds('22:41:56.321'); // 321

Century-related getters

getCenturyStart()

Gets century start date from a given date.

Sample usage
import { getCenturyStart } from '@wojtekmaj/date-utils';

getCenturyStart(new Date(2019, 0, 1)); // new Date(2001, 0, 1)

getCenturyEnd()

Gets century start date from a given date.

Sample usage
import { getCenturyEnd } from '@wojtekmaj/date-utils';

getCenturyEnd(new Date(2019, 0, 1)); // new Date(2100, 12, 31, 23, 59, 999)

getPreviousCenturyStart()

Gets previous century start date from a given date.

Sample usage
import { getPreviousCenturyStart } from '@wojtekmaj/date-utils';

getPreviousCenturyStart(new Date(2019, 0, 1)); // new Date(1901, 0, 1)

getPreviousCenturyEnd()

Gets century start date from a given date.

Sample usage
import { getPreviousCenturyEnd } from '@wojtekmaj/date-utils';

getPreviousCenturyEnd(new Date(2019, 0, 1)); // new Date(2000, 12, 31, 23, 59, 999)

getNextCenturyStart()

Gets next century start date from a given date.

Sample usage
import { getNextCenturyStart } from '@wojtekmaj/date-utils';

getNextCenturyStart(new Date(2019, 0, 1)); // new Date(2101, 0, 1)

getNextCenturyEnd()

Gets next century start date from a given date.

Sample usage
import { getNextCenturyEnd } from '@wojtekmaj/date-utils';

getNextCenturyEnd(new Date(2019, 0, 1)); // new Date(2200, 12, 31, 23, 59, 999)

getCenturyRange()

Gets century start and end dates from a given date. Returns an array of values equal to the ones returned by getCenturyStart() and getCenturyEnd().

Sample usage
import { getCenturyRange } from '@wojtekmaj/date-utils';

getCenturyRange(new Date(2019, 0, 1)); // [new Date(2001, 0, 1), new Date(2100, 12, 31, 23, 59, 999)

Decade-related getters

getDecadeStart()

Gets decade start date from a given date.

Sample usage
import { getDecadeStart } from '@wojtekmaj/date-utils';

getDecadeStart(new Date(2019, 0, 1)); // new Date(2011, 0, 1)

getDecadeEnd()

Gets decade start date from a given date.

Sample usage
import { getDecadeEnd } from '@wojtekmaj/date-utils';

getDecadeEnd(new Date(2019, 0, 1)); // new Date(2020, 12, 31, 23, 59, 999)

getPreviousDecadeStart()

Gets previous decade start date from a given date.

Sample usage
import { getPreviousDecadeStart } from '@wojtekmaj/date-utils';

getPreviousDecadeStart(new Date(2019, 0, 1)); // new Date(2001, 0, 1)

getPreviousDecadeEnd()

Gets decade start date from a given date.

Sample usage
import { getPreviousDecadeEnd } from '@wojtekmaj/date-utils';

getPreviousDecadeEnd(new Date(2019, 0, 1)); // new Date(2010, 12, 31, 23, 59, 999)

getNextDecadeStart()

Gets next decade start date from a given date.

Sample usage
import { getNextDecadeStart } from '@wojtekmaj/date-utils';

getNextDecadeStart(new Date(2019, 0, 1)); // new Date(2021, 0, 1)

getNextDecadeEnd()

Gets next decade start date from a given date.

Sample usage
import { getNextDecadeEnd } from '@wojtekmaj/date-utils';

getNextDecadeEnd(new Date(2019, 0, 1)); // new Date(2030, 12, 31, 23, 59, 999)

getDecadeRange()

Gets decade start and end dates from a given date. Returns an array of values equal to the ones returned by getDecadeStart() and getDecadeEnd().

Sample usage
import { getDecadeRange } from '@wojtekmaj/date-utils';

getDecadeRange(new Date(2019, 0, 1)); // [new Date(2011, 0, 1), new Date(2020, 12, 31, 23, 59, 999)

Year-related getters

getYearStart()

Gets year start date from a given date.

Sample usage
import { getYearStart } from '@wojtekmaj/date-utils';

getYearStart(new Date(2019, 6, 1)); // new Date(2019, 0, 1)

getYearEnd()

Gets year start date from a given date.

Sample usage
import { getYearEnd } from '@wojtekmaj/date-utils';

getYearEnd(new Date(2019, 6, 1)); // new Date(2019, 12, 31, 23, 59, 999)

getPreviousYearStart()

Gets previous year start date from a given date.

Sample usage
import { getPreviousYearStart } from '@wojtekmaj/date-utils';

getPreviousYearStart(new Date(2019, 6, 1)); // new Date(2018, 0, 1)

getPreviousYearEnd()

Gets year start date from a given date.

Sample usage
import { getPreviousYearEnd } from '@wojtekmaj/date-utils';

getPreviousYearEnd(new Date(2019, 6, 1)); // new Date(2018, 12, 31, 23, 59, 999)

getNextYearStart()

Gets next year start date from a given date.

Sample usage
import { getNextYearStart } from '@wojtekmaj/date-utils';

getNextYearStart(new Date(2019, 6, 1)); // new Date(2020, 0, 1)

getNextYearEnd()

Gets next year start date from a given date.

Sample usage
import { getNextYearEnd } from '@wojtekmaj/date-utils';

getNextYearEnd(new Date(2019, 6, 1)); // new Date(2020, 12, 31, 23, 59, 999)

getYearRange()

Gets year start and end dates from a given date. Returns an array of values equal to the ones returned by getYearStart() and getYearEnd().

Sample usage
import { getYearRange } from '@wojtekmaj/date-utils';

getYearRange(new Date(2019, 6, 1)); // [new Date(2019, 0, 1), new Date(2019, 12, 31, 23, 59, 999)

Month-related getters

getMonthStart()

Gets month start date from a given date.

Sample usage
import { getMonthStart } from '@wojtekmaj/date-utils';

getMonthStart(new Date(2019, 6, 15)); // new Date(2019, 6, 1)

getMonthEnd()

Gets month start date from a given date.

Sample usage
import { getMonthEnd } from '@wojtekmaj/date-utils';

getMonthEnd(new Date(2019, 6, 15)); // new Date(2019, 6, 31, 23, 59, 999)

getPreviousMonthStart()

Gets previous month start date from a given date.

Sample usage
import { getPreviousMonthStart } from '@wojtekmaj/date-utils';

getPreviousMonthStart(new Date(2019, 6, 15)); // new Date(2019, 5, 1)

getPreviousMonthEnd()

Gets month start date from a given date.

Sample usage
import { getPreviousMonthEnd } from '@wojtekmaj/date-utils';

getPreviousMonthEnd(new Date(2019, 6, 15)); // new Date(2019, 5, 30, 23, 59, 999)

getNextMonthStart()

Gets next month start date from a given date.

Sample usage
import { getNextMonthStart } from '@wojtekmaj/date-utils';

getNextMonthStart(new Date(2019, 6, 15)); // new Date(2019, 7, 1)

getNextMonthEnd()

Gets next month start date from a given date.

Sample usage
import { getNextMonthEnd } from '@wojtekmaj/date-utils';

getNextMonthEnd(new Date(2019, 6, 15)); // new Date(2019, 7, 31, 23, 59, 999)

getMonthRange()

Gets month start and end dates from a given date. Returns an array of values equal to the ones returned by getMonthStart() and getMonthEnd().

Sample usage
import { getMonthRange } from '@wojtekmaj/date-utils';

getMonthRange(new Date(2019, 6, 15)); // [new Date(2019, 6, 1), new Date(2019, 6, 31, 23, 59, 999)

Day-related getters

getDayStart()

Gets day start date from a given date.

Sample usage
import { getDayStart } from '@wojtekmaj/date-utils';

getDayStart(new Date(2019, 6, 15, 12)); // new Date(2019, 6, 15)

getDayEnd()

Gets day start date from a given date.

Sample usage
import { getDayEnd } from '@wojtekmaj/date-utils';

getDayEnd(new Date(2019, 6, 15, 12)); // new Date(2019, 6, 15, 23, 59, 999)

getPreviousDayStart()

Gets previous day start date from a given date.

Sample usage
import { getPreviousDayStart } from '@wojtekmaj/date-utils';

getPreviousDayStart(new Date(2019, 6, 15, 12)); // new Date(2019, 6, 14)

getPreviousDayEnd()

Gets day start date from a given date.

Sample usage
import { getPreviousDayEnd } from '@wojtekmaj/date-utils';

getPreviousDayEnd(new Date(2019, 6, 15, 12)); // new Date(2019, 6, 14, 23, 59, 999)

getNextDayStart()

Gets next day start date from a given date.

Sample usage
import { getNextDayStart } from '@wojtekmaj/date-utils';

getNextDayStart(new Date(2019, 6, 15, 12)); // new Date(2019, 6, 16)

getNextDayEnd()

Gets next day start date from a given date.

Sample usage
import { getNextDayEnd } from '@wojtekmaj/date-utils';

getNextDayEnd(new Date(2019, 6, 15, 12)); // new Date(2019, 6, 16, 23, 59, 999)

getDayRange()

Gets day start and end dates from a given date. Returns an array of values equal to the ones returned by getDayStart() and getDayEnd().

Sample usage
import { getDayRange } from '@wojtekmaj/date-utils';

getDayRange(new Date(2019, 6, 15, 12)); // [new Date(2019, 6, 15), new Date(2019, 6, 15, 23, 59, 999)

Other

getDaysInMonth()

Gets number of days in a month from a given date.

Sample usage
import { getDaysInMonth } from '@wojtekmaj/date-utils';

getDaysInMonth(new Date(2019, 0, 15)); // 31

getHoursMinutes()

Returns local hours and minutes (hh:mm).

Sample usage
import { getHoursMinutes } from '@wojtekmaj/date-utils';

getHoursMinutes(new Date(2019, 0, 15, 16, 4)); // "16:04"

getHoursMinutesSeconds()

Returns local hours, minutes and seconds (hh:mm:ss).

Sample usage
import { getHoursMinutesSeconds } from '@wojtekmaj/date-utils';

getHoursMinutesSeconds(new Date(2019, 0, 15, 16, 4, 41)); // "16:04:41"

getISOLocalMonth()

Returns local month in ISO-like format (YYYY-MM).

Sample usage
import { getISOLocalMonth } from '@wojtekmaj/date-utils';

getISOLocalMonth(new Date(2019, 0, 15)); // "2019-01"

getISOLocalDate()

Returns local date in ISO-like format (YYYY-MM-DD).

Sample usage
import { getISOLocalDate } from '@wojtekmaj/date-utils';

getISOLocalDate(new Date(2019, 0, 15)); // "2019-01-15"

getISOLocalDateTime()

Returns local date & time in ISO-like format (YYYY-MM-DDThh:mm:ss).

Sample usage
import { getISOLocalDateTime } from '@wojtekmaj/date-utils';

getISOLocalDateTime(new Date(2019, 0, 15, 16, 4, 41)); // "2019-01-15T16:04:41"

License

The MIT License.

Author

Wojciech Maj Wojciech Maj

date-utils's People

Contributors

akihironagai avatar dependabot[bot] avatar felixmosh avatar wojtekmaj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

date-utils's Issues

Adding functions

Hi there, thanks for the amazing lib.
I would like to add more functions to this library.

updated README

https://github.com/leesei/date-utils/commit/a04b82e7ed9b29cbf26b4c018a01650346308443
add ISO/ISO-like strings section

week number

https://www.wikiwand.com/en/ISO_week_date
Will base on implementation in https://stackoverflow.com/a/6117889
Tests will be added with Wiki's data.

Please comment on these API:

getISOWeekNumber(date: Date | string) => number  (50)
getISOWeek(date: Date | string) => string        ("W50")
getISOWeekDay(date: Date | string) => string     ("W504" or "W50-4")
getISOWeekFull(date: Date | string) => string    ("2020W50")
getISOWeekDayFull(date: Date | string) => string ("20202020W504" or "20202020W50-4")

getMonday

/**
 * Gets Monday in the same week.
 *
 * @param {Date} date Date to get Monday from.
 * @param {Date} startWithSunday Whether week starts with Sunday
 */
export const getMonday = (date, startWithSunday = false) => {
  const dayStartDate = getDayStart(date);
  const day = dayStartDate.getDay();
  let mondayRel = -(day - 1); // default value
  if (day === 0) {
    // adjust when day is Sunday
    mondayRel = (startWithSunday)? 1 : -6;
  }
  console.log("Monday is %d days difference", mondayRel);

  return getPreviousDayStart(dayStartDate, mondayRel);
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.