-
Notifications
You must be signed in to change notification settings - Fork 1
/
dateutil.js
68 lines (60 loc) · 1.65 KB
/
dateutil.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
"use strict";
module.exports = {
parse : parseDate,
formatDate : formatDate,
formatTime : formatTime,
formatMis : formatMis
};
/*
var rePattern = [
[/yyyy/i, '(\d{4})'],
[/yy/i, '(\d{2})'],
[/MM/, '(\d{2})'],
[/dd/i, '(\d{2})'],
[/hh/i, '(\d{2})'],
[/mm/i, '(\d{2})'],
[/ss/i, '(\d{2})'],
[/ms/i, '(\d{3})']
];
*/
var parseRegex = /.+\.(\d{4})(\d{2})(\d{2})(?:_(\d{2})(\d{2})(\d{2}))?(?:_(\d{3}))?/;
function parseDate(datestr){
var matches = datestr.match(parseRegex);
var params = [];
if (matches && matches.length === 8) {
for (var i=0; i<7; ++i) {
params[i] = parseInt(matches[i+1]) || 0;
}
// month starts from 0
params[1]--;
return new Date(Date.UTC.apply(Date, params));
}
}
//function createRegexPattern(format) {
// replace all date related patterns with regex capture group and record the sequence
//var chuncks = [];
//}
function formatDate(date/*, format*/) {
var yyyy = date.getUTCFullYear();
var MM = date.getUTCMonth() + 1;
var dd = date.getUTCDate();
MM = (MM < 10) ? ('0' + MM) : MM;
dd = (dd < 10) ? ('0' + dd) : dd;
return '' + yyyy + MM + dd;
}
function formatTime(date) {
var hh = date.getUTCHours();
var mm = date.getUTCMinutes();
var ss = date.getUTCSeconds();
hh = (hh < 10) ? ('0' + hh) : hh;
mm = (mm < 10) ? ('0' + mm) : mm;
ss = (ss < 10) ? ('0' + ss) : ss;
return '' + hh + mm + ss;
}
function formatMis(date) {
var mis = date.getUTCMilliseconds();
mis = (mis < 10) ? ('00' + mis) : ((mis < 100) ? '0' + mis : mis);
return '' + mis;
}
//var pattern = new RegExp('(' + escapePattern(fa.pattern) + ')\-?(\d*)');
//console.log(parseDate('tomcat.20150102.log'));