@swaray21/date-utils 0.0.0-stage → 1.0.1
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 +80 -3
- package/index.js +86 -0
- package/package.json +22 -4
package/LICENSE
ADDED
|
File without changes
|
package/README.md
CHANGED
|
@@ -1,3 +1,80 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# Date Utils
|
|
2
|
+
|
|
3
|
+
A lightweight JavaScript package that provides simple date and time utility functions.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install the package using npm:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @swaray21/date-utils
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
Import the functions into your JavaScript project:
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
const {
|
|
19
|
+
formatDate,
|
|
20
|
+
calculateAge,
|
|
21
|
+
getDaysBetween,
|
|
22
|
+
isWeekend,
|
|
23
|
+
addDays
|
|
24
|
+
} = require("@swaray21/date-utils");
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Available Functions
|
|
28
|
+
|
|
29
|
+
### 1. formatDate(date)
|
|
30
|
+
|
|
31
|
+
Formats a date into DD/MM/YYYY.
|
|
32
|
+
|
|
33
|
+
```javascript
|
|
34
|
+
formatDate("2026-10-31");
|
|
35
|
+
// Output: 31/10/2026
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### 2. calculateAge(dateOfBirth)
|
|
39
|
+
|
|
40
|
+
Calculates a person's age from their date of birth.
|
|
41
|
+
|
|
42
|
+
```javascript
|
|
43
|
+
calculateAge("2000-01-01");
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### 3. getDaysBetween(startDate, endDate)
|
|
47
|
+
|
|
48
|
+
Calculates the number of days between two dates.
|
|
49
|
+
|
|
50
|
+
```javascript
|
|
51
|
+
getDaysBetween("2026-10-01", "2026-10-31");
|
|
52
|
+
// Output: 30
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### 4. isWeekend(date)
|
|
56
|
+
|
|
57
|
+
Checks whether a date falls on Saturday or Sunday.
|
|
58
|
+
|
|
59
|
+
```javascript
|
|
60
|
+
isWeekend("2026-10-31");
|
|
61
|
+
// Output: true
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 5. addDays(date, days)
|
|
65
|
+
|
|
66
|
+
Adds a specified number of days to a date.
|
|
67
|
+
|
|
68
|
+
```javascript
|
|
69
|
+
addDays("2026-10-31", 5);
|
|
70
|
+
// Returns a Date object representing November 5, 2026.
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Requirements
|
|
74
|
+
|
|
75
|
+
- Node.js
|
|
76
|
+
- npm
|
|
77
|
+
|
|
78
|
+
## License
|
|
79
|
+
|
|
80
|
+
MIT
|
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
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
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
|
+
]
|
|
24
|
+
}
|