@swaray21/date-utils 0.0.0-stage → 1.0.0
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/LICENSE +0 -0
- package/README.md +0 -3
- package/index.js +86 -0
- package/package.json +21 -3
package/LICENSE
ADDED
|
File without changes
|
package/README.md
CHANGED
package/index.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Format a date into DD/MM/YYYY
|
|
2
|
+
function formatDate(date) {
|
|
3
|
+
const newDate = new Date(date);
|
|
4
|
+
|
|
5
|
+
if (isNaN(newDate.getTime())) {
|
|
6
|
+
return "Invalid Date";
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const day = String(newDate.getDate()).padStart(2, "0");
|
|
10
|
+
const month = String(newDate.getMonth() + 1).padStart(2, "0");
|
|
11
|
+
const year = newDate.getFullYear();
|
|
12
|
+
|
|
13
|
+
return `${day}/${month}/${year}`;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Calculate age from date of birth
|
|
17
|
+
function calculateAge(dateOfBirth) {
|
|
18
|
+
const birthDate = new Date(dateOfBirth);
|
|
19
|
+
|
|
20
|
+
if (isNaN(birthDate.getTime())) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const today = new Date();
|
|
25
|
+
let age = today.getFullYear() - birthDate.getFullYear();
|
|
26
|
+
|
|
27
|
+
const hasBirthdayPassed =
|
|
28
|
+
today.getMonth() > birthDate.getMonth() ||
|
|
29
|
+
(today.getMonth() === birthDate.getMonth() &&
|
|
30
|
+
today.getDate() >= birthDate.getDate());
|
|
31
|
+
|
|
32
|
+
if (!hasBirthdayPassed) {
|
|
33
|
+
age--;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return age;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Calculate the number of days between two dates
|
|
40
|
+
function getDaysBetween(startDate, endDate) {
|
|
41
|
+
const start = new Date(startDate);
|
|
42
|
+
const end = new Date(endDate);
|
|
43
|
+
|
|
44
|
+
if (isNaN(start.getTime()) || isNaN(end.getTime())) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const difference = Math.abs(end - start);
|
|
49
|
+
|
|
50
|
+
return Math.floor(difference / (1000 * 60 * 60 * 24));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Check whether a date is a weekend
|
|
54
|
+
function isWeekend(date) {
|
|
55
|
+
const selectedDate = new Date(date);
|
|
56
|
+
|
|
57
|
+
if (isNaN(selectedDate.getTime())) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const day = selectedDate.getDay();
|
|
62
|
+
|
|
63
|
+
return day === 0 || day === 6;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Add days to a date
|
|
67
|
+
function addDays(date, days) {
|
|
68
|
+
const result = new Date(date);
|
|
69
|
+
|
|
70
|
+
if (isNaN(result.getTime()) || !Number.isInteger(days)) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
result.setDate(result.getDate() + days);
|
|
75
|
+
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Make functions available to other projects
|
|
80
|
+
module.exports = {
|
|
81
|
+
formatDate,
|
|
82
|
+
calculateAge,
|
|
83
|
+
getDaysBetween,
|
|
84
|
+
isWeekend,
|
|
85
|
+
addDays
|
|
86
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@swaray21/date-utils",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A lightweight JavaScript date and time utility library",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "commonjs",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "node test/test.js"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"date",
|
|
12
|
+
"time",
|
|
13
|
+
"javascript",
|
|
14
|
+
"utilities",
|
|
15
|
+
"date-utils"
|
|
16
|
+
],
|
|
17
|
+
"author": "Lahai Simone Swaray",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"files": [
|
|
20
|
+
"index.js",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
]
|
|
6
24
|
}
|