Mercurial > eldonilo > blog
comparison date.js @ 0:f62b5c395a48
Initial commit.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sat, 04 Jun 2011 05:02:47 +0200 |
parents | |
children | 82905edac9d8 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f62b5c395a48 |
---|---|
1 Date.prototype.set8601 = function (string) { | |
2 var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" + | |
3 "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" + | |
4 "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?"; | |
5 var d = string.match(new RegExp(regexp)); | |
6 | |
7 var offset = 0; | |
8 var date = new Date(d[1], 0, 1); | |
9 | |
10 if (d[3]) { date.setMonth(d[3] - 1); } | |
11 if (d[5]) { date.setDate(d[5]); } | |
12 if (d[7]) { date.setHours(d[7]); } | |
13 if (d[8]) { date.setMinutes(d[8]); } | |
14 if (d[10]) { date.setSeconds(d[10]); } | |
15 if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); } | |
16 if (d[14]) { | |
17 offset = (Number(d[16]) * 60) + Number(d[17]); | |
18 offset *= ((d[15] == '-') ? 1 : -1); | |
19 } | |
20 | |
21 offset -= date.getTimezoneOffset(); | |
22 var time = (Number(date) + (offset * 60 * 1000)); | |
23 this.setTime(Number(time)); | |
24 | |
25 return this; | |
26 } | |
27 | |
28 Date.prototype.to8601 = function(){ | |
29 function pad(n){return n<10 ? '0'+n : n} | |
30 return this.getUTCFullYear()+'-' | |
31 + pad(this.getUTCMonth()+1)+'-' | |
32 + pad(this.getUTCDate())+'T' | |
33 + pad(this.getUTCHours())+':' | |
34 + pad(this.getUTCMinutes())+':' | |
35 + pad(this.getUTCSeconds())+'Z'; | |
36 } | |
37 | |
38 Date.prototype.getRelative = function(){ | |
39 const s = 1000, m = s*60, h = m*60, d = h*24, y = d*365, M = y/12, | |
40 ref = Date.now(), | |
41 input = this.getTime(), | |
42 delta = ref - input, | |
43 year = Math.round((delta/y)*10)/10, | |
44 month = Math.round((delta/M)*10)/10, | |
45 day = Math.round((delta/d)*10)/10, | |
46 hour = Math.round((delta/h)*10)/10, | |
47 minute = Math.round((delta/m)*10)/10; | |
48 | |
49 if (year == 1) | |
50 return "a year ago"; | |
51 if (year > 1) | |
52 return Math.round(year)+" years ago"; | |
53 | |
54 if (month == 1) | |
55 return "a month ago"; | |
56 if (month > 1) | |
57 return Math.round(month)+" months ago"; | |
58 | |
59 if (day == 1) | |
60 return "today"; | |
61 if (day > 1) | |
62 return Math.round(day)+" days ago"; | |
63 | |
64 if (hour == 1) | |
65 return "an hour ago"; | |
66 if (hour > 1) | |
67 return Math.round(hour)+" hours ago"; | |
68 | |
69 if (minute == 1) | |
70 return "a minute ago"; | |
71 if (minute > 1) | |
72 return Math.round(minute)+" minutes ago"; | |
73 | |
74 return "just now"; | |
75 }; |