forked from Mazhar7262/ParseTime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parse.js
79 lines (74 loc) · 2.9 KB
/
Parse.js
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
function parseTime(timeString) {
const now = new Date();
const [modifier, snap] = timeString.split('@');
const modifierRegex = /[+-]?\d+[smhdwy]?/g;
const modifiers = modifier.match(modifierRegex);
let result = new Date(now);
if(modifiers != null)
{
modifiers.forEach(mod => {
const operator = mod[0];
const value = parseInt(mod.slice(1, -1));
const unit = mod.slice(-1);
switch (unit) {
case 's':
result.setSeconds(result.getSeconds() + (operator === '+' ? value : -value));
break;
case 'm':
result.setMinutes(result.getMinutes() + (operator === '+' ? value : -value));
break;
case 'h':
result.setHours(result.getHours() + (operator === '+' ? value : -value));
break;
case 'd':
result.setDate(result.getDate() + (operator === '+' ? value : -value));
break;
case 'w':
result.setDate(result.getDate() + (operator === '+' ? value * 7 : -value * 7));
break;
case 'y':
result.setFullYear(result.getFullYear() + (operator === '+' ? value : -value));
break;
default:
throw new Error('Invalid time unit');
}
});
}
if (snap) {
switch (snap) {
case 's':
result.setMilliseconds(0);
break;
case 'm':
result.setSeconds(0, 0);
break;
case 'h':
result.setMinutes(0, 0, 0);
break;
case 'd':
result.setHours(0, 0, 0, 0);
break;
case 'w':
result.setDate(result.getDate() - result.getDay());
result.setHours(0, 0, 0, 0);
break;
case 'mon':
result.setDate(1);
result.setHours(0, 0, 0, 0);
break;
case 'y':
result.setMonth(0, 1);
result.setHours(0, 0, 0, 0);
break;
default:
throw new Error('Invalid snap unit');
}
}
return result.toISOString();
}
// Example:
console.log(parseTime('now()+1d')); // Output will be current time + 1 day
console.log(parseTime('now()-1d')); // Output will be current time - 1 day
console.log(parseTime('now()@d')); // Output will be the start of the current day
console.log(parseTime('now()-1y@mon')); // Output will be the start of the month one year ago
console.log(parseTime('now()+10d+12h')); // Output will be current time + 10 days and 12 hours