@oluwanifemi/date-formatt 0.0.1 → 0.0.2
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/index.js +36 -40
- package/package.json +1 -1
- package/validator.js +43 -0
package/index.js
CHANGED
|
@@ -1,61 +1,57 @@
|
|
|
1
|
-
const
|
|
1
|
+
const validator = require("./validator")
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
// Date portion --> "YYYY-MM-DD" --> "2026-03-09"
|
|
9
|
-
// Time portion --> "HH:MM:SS.milliseconds" --> "11:42:13.260Z"
|
|
10
|
-
// T --> Indicator for time portion starting point
|
|
11
|
-
// Z --> "Zulu Time"/"Coordinated Universal Time (UTC)"
|
|
12
|
-
|
|
13
|
-
const d = new Date("October 13, 2014 11:13:00")
|
|
14
|
-
// console.log(d) --> this also returns the date in ISO 8601 format
|
|
15
|
-
// C:\Users\FullstackCreator\Documents\Programming\date-formatt
|
|
16
|
-
|
|
17
|
-
function iso(date) {
|
|
18
|
-
return date.toISOString().split("T")[0]
|
|
3
|
+
function iso(input) {
|
|
4
|
+
if(typeof(input) === 'string'){
|
|
5
|
+
let date = validator.validate(input)
|
|
6
|
+
return date.toISOString().split("T")[0]
|
|
7
|
+
}
|
|
19
8
|
}
|
|
20
9
|
|
|
21
|
-
function short(
|
|
22
|
-
|
|
10
|
+
function short(input) {
|
|
11
|
+
if(typeof(input) === 'string'){
|
|
12
|
+
let date = validator.validate(input)
|
|
13
|
+
return date.toLocaleDateString("en", {
|
|
23
14
|
day: "numeric",
|
|
24
15
|
month: "short",
|
|
25
16
|
year: "numeric"
|
|
26
17
|
})
|
|
18
|
+
}
|
|
19
|
+
|
|
27
20
|
}
|
|
28
21
|
|
|
29
|
-
function long(
|
|
30
|
-
|
|
22
|
+
function long(input) {
|
|
23
|
+
if(typeof(input) === 'string') {
|
|
24
|
+
let date = validator.validate(input)
|
|
25
|
+
return date.toLocaleDateString("en", {
|
|
31
26
|
weekday: "long",
|
|
32
27
|
month: "long",
|
|
33
28
|
day: "numeric",
|
|
34
29
|
year: "numeric"
|
|
35
30
|
})
|
|
31
|
+
}
|
|
36
32
|
}
|
|
37
33
|
|
|
38
|
-
function dateTime(date) {
|
|
39
|
-
return date.toLocaleString("en", {
|
|
40
|
-
day: "numeric",
|
|
41
|
-
month: "short",
|
|
42
|
-
year: "numeric",
|
|
43
|
-
hour: "numeric",
|
|
44
|
-
minute: "2-digit"
|
|
45
|
-
})
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function time(date) {
|
|
49
|
-
return date.toLocaleTimeString("en", {
|
|
50
|
-
hour: "numeric",
|
|
51
|
-
minute: "2-digit"
|
|
52
|
-
})
|
|
53
|
-
}
|
|
54
34
|
|
|
55
35
|
module.exports = {
|
|
56
36
|
iso,
|
|
57
37
|
short,
|
|
58
38
|
long,
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
// const { module } = require("browserify/lib/builtins")
|
|
44
|
+
|
|
45
|
+
// const date = new Date()
|
|
46
|
+
// console.log(date) --> this returns a date in the ISO 8601 format,
|
|
47
|
+
// commonly used in databases, APIs and JavaScript
|
|
48
|
+
|
|
49
|
+
// e.g "2026-03-09T11:42:13.260Z"
|
|
50
|
+
// Date portion --> "YYYY-MM-DD" --> "2026-03-09"
|
|
51
|
+
// Time portion --> "HH:MM:SS.milliseconds" --> "11:42:13.260Z"
|
|
52
|
+
// T --> Indicator for time portion starting point
|
|
53
|
+
// Z --> "Zulu Time"/"Coordinated Universal Time (UTC)"
|
|
54
|
+
|
|
55
|
+
// const d = new Date("October 13, 2014 11:13:00")
|
|
56
|
+
// console.log(d) --> this also returns the date in ISO 8601 format
|
|
57
|
+
// C:\Users\FullstackCreator\Documents\Programming\date-formatt
|
package/package.json
CHANGED
package/validator.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const fakeDateError = [null, "", 'boolean', undefined]
|
|
2
|
+
|
|
3
|
+
function validate(input) {
|
|
4
|
+
const inValid = fakeDateError.some(errorType => input === errorType || typeof(input) === 'boolean')
|
|
5
|
+
if(inValid) {
|
|
6
|
+
throw new TypeError("Input cannot be a null, boolean or undefined.")
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
let inputArray
|
|
11
|
+
|
|
12
|
+
try {
|
|
13
|
+
inputArray = input.split('-')
|
|
14
|
+
} catch (error) {
|
|
15
|
+
throw new TypeError(error + ": Your input should be in the format 'YYYY - MM - DD'")
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const year = inputArray[0]
|
|
19
|
+
const month = inputArray[1]
|
|
20
|
+
const day = inputArray[2]
|
|
21
|
+
|
|
22
|
+
const date = new Date(input)
|
|
23
|
+
|
|
24
|
+
if(date.getFullYear() !== Number(year) || date.getMonth() +1 !== Number(month) || date.getDate() !== Number(day)){
|
|
25
|
+
console.log(date.getFullYear())
|
|
26
|
+
console.log(date.getMonth())
|
|
27
|
+
console.log(date.getDate())
|
|
28
|
+
console.log(Number(year))
|
|
29
|
+
console.log(Number(month))
|
|
30
|
+
console.log(Number(day))
|
|
31
|
+
throw new TypeError("Your input should be in the format 'YYYY - MM - DD' and not 'YYYY-DD-MM'")
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if(Number.isNaN(date.getTime())) {
|
|
35
|
+
throw new TypeError("Input must be of a valid date type.")
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return date
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
module.exports = {
|
|
42
|
+
validate
|
|
43
|
+
}
|