forked from zuriby/Faker.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.js
42 lines (31 loc) · 1.28 KB
/
date.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
var Faker = require('../index');
var date = {
past: function (years, refDate) {
var date = (refDate) ? new Date(Date.parse(refDate)) : new Date();
var past = date.getTime();
past -= Faker.random.number(years) * 365 * 3600 * 1000; // some time from now to N years ago, in milliseconds
date.setTime(past)
return date.toJSON();
},
future: function (years, refDate) {
var date = (refDate) ? new Date(Date.parse(refDate)) : new Date();
var future = date.getTime();
future += Faker.random.number(years) * 365 * 3600 * 1000 + 1000; // some time from now to N years later, in milliseconds
date.setTime(future)
return date.toJSON();
},
between: function(from, to) {
var fromMilli = Date.parse(from);
var dateOffset = Faker.random.number(Date.parse(to) - fromMilli);
var newDate = new Date(fromMilli + dateOffset);
return newDate.toJSON();
},
recent: function (days) {
var date = new Date();
var future = date.getTime();
future -= Faker.random.number(days) * 24 * 60 * 60 * 1000; // some time from now to N days ago, in milliseconds
date.setTime(future)
return date.toJSON();
}
};
module.exports = date;