summaryrefslogtreecommitdiff
path: root/date-fns/src/isThisWeek/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'date-fns/src/isThisWeek/index.ts')
-rw-r--r--date-fns/src/isThisWeek/index.ts48
1 files changed, 48 insertions, 0 deletions
diff --git a/date-fns/src/isThisWeek/index.ts b/date-fns/src/isThisWeek/index.ts
new file mode 100644
index 0000000..4d08327
--- /dev/null
+++ b/date-fns/src/isThisWeek/index.ts
@@ -0,0 +1,48 @@
+import isSameWeek from '../isSameWeek/index'
+import requiredArgs from '../_lib/requiredArgs/index'
+import { LocaleOptions, WeekStartOptions } from '../types'
+
+/**
+ * @name isThisWeek
+ * @category Week Helpers
+ * @summary Is the given date in the same week as the current date?
+ * @pure false
+ *
+ * @description
+ * Is the given date in the same week as the current date?
+ *
+ * > ⚠️ Please note that this function is not present in the FP submodule as
+ * > it uses `Date.now()` internally hence impure and can't be safely curried.
+ *
+ * ### v2.0.0 breaking changes:
+ *
+ * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
+ *
+ * @param {Date|Number} date - the date to check
+ * @param {Object} [options] - the object with options
+ * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
+ * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
+ * @returns {Boolean} the date is in this week
+ * @throws {TypeError} 1 argument required
+ * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
+ *
+ * @example
+ * // If today is 25 September 2014, is 21 September 2014 in this week?
+ * var result = isThisWeek(new Date(2014, 8, 21))
+ * //=> true
+ *
+ * @example
+ * // If today is 25 September 2014 and week starts with Monday
+ * // is 21 September 2014 in this week?
+ * var result = isThisWeek(new Date(2014, 8, 21), { weekStartsOn: 1 })
+ * //=> false
+ */
+
+export default function isThisWeek(
+ dirtyDate: Date | number,
+ options?: LocaleOptions & WeekStartOptions
+): boolean {
+ requiredArgs(1, arguments)
+
+ return isSameWeek(dirtyDate, Date.now(), options)
+}