-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate.uiParser.js
More file actions
89 lines (76 loc) · 2.28 KB
/
date.uiParser.js
File metadata and controls
89 lines (76 loc) · 2.28 KB
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
80
81
82
83
84
85
86
87
88
/**
* Flexible date parse function
* The date is stored in the object
* @returns the format string which was used
*/
Date.prototype.setUIDate = function (dateStr) {
// [[yy]yy-]mm-dd
// 2 3 4 5
var regexp = "^(([0-9]{2})?([0-9]{2})-)?([0-9]{1,2})-([0-9]{1,2})$";
var d = dateStr.match(new RegExp(regexp));
if (d) {
if (d[3]) { this.setYear( (d[2]?d[2]:'20')*100 + d[3]*1); }
if (d[4]) { this.setMonth(d[4] - 1); }
if (d[5]) { this.setDate(d[5]); }
return 'yyyy-mm-dd';
}
// mm/dd[/[yy]yy]
// 1 2 4 5
var regexp = "^([0-9]{1,2})/([0-9]{1,2})(/([0-9]{2})?([0-9]{2}))?$";
var d = dateStr.match(new RegExp(regexp));
if (d) {
if (d[5]) { this.setYear( (d[4]?d[4]:'20')*100 + d[5]*1); }
if (d[1]) { this.setMonth(d[1] - 1); }
if (d[2]) { this.setDate(d[2]); }
return 'mm/dd/yyyy';
}
// dd.mm[.[yy]yy]
// 1 2 4 5
var regexp = "^([0-9]{1,2})\\.([0-9]{1,2})(\\.([0-9]{2})?([0-9]{2}))?$";
var d = dateStr.match(new RegExp(regexp));
if (d) {
if (d[5]) { this.setYear( (d[4]?d[4]:'20')*100 + d[5]*1); }
if (d[2]) { this.setMonth(d[2] - 1); }
if (d[1]) { this.setDate(d[1]); }
return 'dd.mm.yyyy';
}
throw 'unknown date format. use yyyy-mm-dd, mm/dd/yyyy or dd.mm.yyyy';
}
/**
* Flexible time parse function
* The time is stored in the object
* @returns the format string which was used
*/
Date.prototype.setUITime = function (timeStr) {
// HH[:MM[:SS]]
// 1 3 5
var regexp = "^([0-9]{1,2})?(:([0-9]{2})(:([0-9]{2}))?)?$";
var d = timeStr.match(new RegExp(regexp));
if (d) {
if (d[1]) { this.setHours(d[1]); } else { this.setHours(0); }
if (d[3]) { this.setMinutes(d[3]); } else { this.setMinutes(0); }
if (d[5]) { this.setSeconds(d[5]); } else { this.setSeconds(0); }
this.setMilliseconds(0);
if (d[5])
return 'HH:MM:ss';
else
return 'HH:MM';
}
throw 'unknown time format. use HH:MM or HH:MM:SS';
}
/**
* Usage example (dRef and tRef are jQuery references)
function parseUIDateTime(dRef, tRef) {
var date = new Date();
var s, dFmt, tFmt;
if (s = dRef.val() )
dFmt = date.setUIDate( s );
if (s = tRef.val() )
tFmt = date.setUITime( s );
if (dFmt)
dRef.val(date.format(dFmt));
if (tFmt)
tRef.val(date.format(tFmt));
return date;
}
*/