-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (49 loc) · 2.83 KB
/
index.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
const moment = require('moment');
let timestampStr="";
exports.getTimeStampFromFormattedDate = function(dateNtime){
var now = new Date();
var then = dateNtime;
var msDiff = new Date().getTime() - new Date(then).getTime(); //Future date - current date
var gettingDays = Math.floor(msDiff / (1000 * 60 * 60 * 24));
if (gettingDays >=365 ){
timestampStr = Math.floor(gettingDays/365)+" years ago";
}
else if (gettingDays >= 31) {
timestampStr = Math.floor(gettingDays/31)+" months ago";
}
else if(gettingDays > 0){
timestampStr=gettingDays+" days ago"
}else{
var timestamp = moment.utc(moment(now, "DD/MM/YYYY HH:mm:ss").diff(moment(new Date(then), "DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss");
if (parseInt(timestamp.substring(0, 2)) > 0) timestampStr = timestamp.substring(0, 2) + " hours ago";
else if (parseInt(timestamp.substring(3, 5)) > 0) timestampStr = timestamp.substring(3, 5) + " minutes ago";
else if (parseInt(timestamp.substring(6, 8)) > 0) timestampStr = timestamp.substring(6, 8) + " seconds ago";
else timestampStr = "now";
}
return timestampStr
}
exports.getTimeStampFromDate = function(dateNtime){
var now = new Date();
var then = moment(dateNtime).format("MM/DD/YYYY HH:mm:ss");
var msDiff = new Date().getTime() - new Date(then).getTime(); //Future date - current date
var gettingDays = Math.floor(msDiff / (1000 * 60 * 60 * 24));
if (gettingDays >=365 ){
timestampStr = Math.floor(gettingDays/365)+" years ago";
}
else if (gettingDays >= 31) {
timestampStr = Math.floor(gettingDays/31)+" months ago";
}
else if(gettingDays > 0){
timestampStr=gettingDays+" days ago"
}else{
var timestamp = moment.utc(moment(now, "DD/MM/YYYY HH:mm:ss").diff(moment(then, "DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss");
if (parseInt(timestamp.substring(0, 2)) > 0) timestampStr = timestamp.substring(0, 2) + " hours ago";
else if (parseInt(timestamp.substring(3, 5)) > 0) timestampStr = timestamp.substring(3, 5) + " minutes ago";
else if (parseInt(timestamp.substring(6, 8)) > 0) timestampStr = timestamp.substring(6, 8) + " seconds ago";
else timestampStr = "now";
}
return timestampStr
}
exports.printMsg = function() {
console.log("This is a message from the time-spent package");
}