@tim-code/my-util 0.0.17 → 0.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/time.js +14 -7
package/package.json
CHANGED
package/src/time.js
CHANGED
|
@@ -6,7 +6,7 @@ import { mod } from "./math.js"
|
|
|
6
6
|
* @param {boolean=} $1.floorMinute If true, floors to the nearest minute. If false, floors to the nearest second.
|
|
7
7
|
* @param {number=} $1.timestamp Unix timestamp to use instead of current time.
|
|
8
8
|
* @param {string=} $1.timezone Timezone to use instead of Eastern time.
|
|
9
|
-
* @returns {Object} { timestamp, date, time
|
|
9
|
+
* @returns {Object} { timestamp, date: YYYY-MM-DD, time: HH:mm:ss }
|
|
10
10
|
*/
|
|
11
11
|
export function getEasternTime({
|
|
12
12
|
floorMinute = false,
|
|
@@ -17,7 +17,8 @@ export function getEasternTime({
|
|
|
17
17
|
timestamp = new Date().getTime() / 1000
|
|
18
18
|
}
|
|
19
19
|
timestamp = floorMinute ? Math.floor(timestamp / 60) * 60 : Math.floor(timestamp)
|
|
20
|
-
|
|
20
|
+
// 'en-CA' (English - Canada) formats dates as YYYY-MM-DD and times in 24-hour format by default
|
|
21
|
+
const string = new Date(timestamp * 1000).toLocaleString("en-CA", {
|
|
21
22
|
...(timezone ? { timeZone: timezone } : {}),
|
|
22
23
|
hour12: false,
|
|
23
24
|
year: "numeric",
|
|
@@ -27,9 +28,7 @@ export function getEasternTime({
|
|
|
27
28
|
minute: "2-digit",
|
|
28
29
|
second: "2-digit",
|
|
29
30
|
})
|
|
30
|
-
const [
|
|
31
|
-
const [month, day, year] = americanDate.split("/")
|
|
32
|
-
const date = [year, month, day].join("-")
|
|
31
|
+
const [date, time] = string.split(", ")
|
|
33
32
|
return { timestamp, date, time }
|
|
34
33
|
}
|
|
35
34
|
|
|
@@ -38,13 +37,21 @@ export function getEasternTime({
|
|
|
38
37
|
* @param {Object} $1
|
|
39
38
|
* @param {boolean=} $1.floorMinute If true, floors to the nearest minute. If false, floors to the nearest second.
|
|
40
39
|
* @param {number=} $1.timestamp Unix timestamp to use instead of current time.
|
|
41
|
-
* @param {string=} $1.timezone Timezone to use instead of local time.
|
|
40
|
+
* @param {string=} $1.timezone Timezone to use instead of local time. undefined corresponds to "America/New_York" and "" (falsy) corresponds to local time.
|
|
42
41
|
* @returns {Object} { timestamp, date, time, minute, datetime }
|
|
43
42
|
*/
|
|
44
|
-
export function getTime({ floorMinute, timestamp, timezone =
|
|
43
|
+
export function getTime({ floorMinute, timestamp, timezone = "" } = {}) {
|
|
45
44
|
return getEasternTime({ floorMinute, timestamp, timezone })
|
|
46
45
|
}
|
|
47
46
|
|
|
47
|
+
/**
|
|
48
|
+
* Get today's date in YYYY-MM-DD format.
|
|
49
|
+
* @returns {string}
|
|
50
|
+
*/
|
|
51
|
+
export function today() {
|
|
52
|
+
return getTime().date
|
|
53
|
+
}
|
|
54
|
+
|
|
48
55
|
/**
|
|
49
56
|
* Checks if the string represents a valid YYYY-MM-DD date.
|
|
50
57
|
* This will return false for dates like "2024-02-31".
|