summaryrefslogtreecommitdiff
path: root/std/datetime/mod.ts
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2020-07-14 15:24:17 -0400
committerGitHub <noreply@github.com>2020-07-14 15:24:17 -0400
commitcde4dbb35132848ffece59ef9cfaccff32347124 (patch)
treecc7830968c6decde704c8cfb83c9185193dc698f /std/datetime/mod.ts
parent9eca71caa1674c31f9cc5d4e86c03f10b59e0a00 (diff)
Use dprint for internal formatting (#6682)
Diffstat (limited to 'std/datetime/mod.ts')
-rw-r--r--std/datetime/mod.ts49
1 files changed, 24 insertions, 25 deletions
diff --git a/std/datetime/mod.ts b/std/datetime/mod.ts
index 9665a92b2..fe6fe6b3c 100644
--- a/std/datetime/mod.ts
+++ b/std/datetime/mod.ts
@@ -71,7 +71,7 @@ export type DateTimeFormat =
*/
export function parseDateTime(
datetimeStr: string,
- format: DateTimeFormat
+ format: DateTimeFormat,
): Date {
let m, d, y, ho, mi: string;
let datePattern: RegExp;
@@ -114,8 +114,7 @@ export function parseDateTime(
*/
export function dayOfYear(date: Date): number {
const yearStart = new Date(date.getFullYear(), 0, 0);
- const diff =
- date.getTime() -
+ const diff = date.getTime() -
yearStart.getTime() +
(yearStart.getTimezoneOffset() - date.getTimezoneOffset()) * 60 * 1000;
return Math.floor(diff / DAY);
@@ -135,13 +134,12 @@ export function currentDayOfYear(): number {
*/
export function weekOfYear(date: Date): number {
const workingDate = new Date(
- Date.UTC(date.getFullYear(), date.getMonth(), date.getDate())
+ Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()),
);
const day = workingDate.getUTCDay();
- const nearestThursday =
- workingDate.getUTCDate() +
+ const nearestThursday = workingDate.getUTCDate() +
Day.Thu -
(day === Day.Sun ? DAYS_PER_WEEK : day);
@@ -236,21 +234,19 @@ export type DifferenceOptions = {
export function difference(
from: Date,
to: Date,
- options?: DifferenceOptions
+ options?: DifferenceOptions,
): DifferenceFormat {
- const uniqueUnits = options?.units
- ? [...new Set(options?.units)]
- : [
- "miliseconds",
- "seconds",
- "minutes",
- "hours",
- "days",
- "weeks",
- "months",
- "quarters",
- "years",
- ];
+ const uniqueUnits = options?.units ? [...new Set(options?.units)] : [
+ "miliseconds",
+ "seconds",
+ "minutes",
+ "hours",
+ "days",
+ "weeks",
+ "months",
+ "quarters",
+ "years",
+ ];
const bigger = Math.max(from.getTime(), to.getTime());
const smaller = Math.min(from.getTime(), to.getTime());
@@ -285,14 +281,14 @@ export function difference(
differences.quarters = Math.floor(
(typeof differences.months !== "undefined" &&
differences.months / 4) ||
- calculateMonthsDifference(bigger, smaller) / 4
+ calculateMonthsDifference(bigger, smaller) / 4,
);
break;
case "years":
differences.years = Math.floor(
(typeof differences.months !== "undefined" &&
differences.months / 12) ||
- calculateMonthsDifference(bigger, smaller) / 12
+ calculateMonthsDifference(bigger, smaller) / 12,
);
break;
}
@@ -309,10 +305,13 @@ function calculateMonthsDifference(bigger: number, smaller: number): number {
const calendarDiffrences = Math.abs(yearsDiff * 12 + monthsDiff);
const compareResult = biggerDate > smallerDate ? 1 : -1;
biggerDate.setMonth(
- biggerDate.getMonth() - compareResult * calendarDiffrences
+ biggerDate.getMonth() - compareResult * calendarDiffrences,
);
- const isLastMonthNotFull =
- biggerDate > smallerDate ? 1 : -1 === -compareResult ? 1 : 0;
+ const isLastMonthNotFull = biggerDate > smallerDate
+ ? 1
+ : -1 === -compareResult
+ ? 1
+ : 0;
const months = compareResult * (calendarDiffrences - isLastMonthNotFull);
return months === 0 ? 0 : months;
}