generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtime-utils.ts
More file actions
89 lines (77 loc) · 2.51 KB
/
time-utils.ts
File metadata and controls
89 lines (77 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { moment } from "obsidian";
const DATE_FORMATS = ["YYYY-MM-DDTHH:mm:ss", "YYYY-MM-DDTHH:mm", "YYYY-MM-DD"];
/**
* Gets 12:00 AM of the current day in milliseconds
* @returns - The current time in milliseconds
*/
export const getStartOfTodayMillis = () => {
return (moment as any)().startOf("day").valueOf();
};
/**
* Gets 12:00 AM of the day for the given date in milliseconds
* @param date - The date to get the start of the day for
* @returns - The start of the day in milliseconds
*/
export const getStartOfDayMillis = (date: string) => {
return (moment as any)(date).startOf("day").valueOf();
};
/**
* Gets 12:00 AM of the current week in milliseconds. The week starts on Sunday.
* @returns - The start of the week in milliseconds
*/
export const getStartOfThisWeekMillis = () => {
return (moment as any)().startOf("week").valueOf();
};
export const getMomentDate = (date: string) => {
return (moment as any)(date, DATE_FORMATS, true);
};
export const getDateDaysAgo = (daysAgo: number) => {
return (moment as any)().subtract(daysAgo, "days").format("YYYY-MM-DD");
};
export const getDateDaysAhead = (daysAgo: number) => {
return (moment as any)().add(daysAgo, "days").format("YYYY-MM-DD");
};
/**
* Gets 12:00 AM of the previous week in milliseconds. The week starts on Sunday.
* @returns - The start of the previous week in milliseconds
*/
export const getStartOfLastWeekMillis = () => {
//This is the Sunday the previous week
return (moment as any)().subtract(1, "weeks").startOf("week").valueOf();
};
/**
* Gets the time in milliseconds
* @param date - The date to get the time for.
* @returns - The date in milliseconds
*/
export const getTimeMillis = (date: string) => {
const momentDate = (moment as any)(date, DATE_FORMATS, true);
if (!momentDate.isValid()) {
throw new Error(`Date format not handled: ${date}`);
}
return momentDate.valueOf();
};
/**
* Checks if the date is supported
* @param date - The date to check if it is supported
* @returns - True if the date is supported, false otherwise
*/
export const isDateSupported = (date: string) => {
const momentDate = (moment as any)(date, DATE_FORMATS, true);
return momentDate.isValid();
};
/**
* Gets 11:59 PM of the current day in milliseconds
* @param date - The date to get the end of the day for
* @returns - The end of the day in milliseconds
*/
export const getEndOfDayMillis = (date: string) => {
const day = (moment as any)(date);
day.set({
hour: 23,
minute: 59,
second: 59,
millisecond: 999
});
return day.valueOf();
};