-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvi-datetime.js
More file actions
186 lines (133 loc) · 4.59 KB
/
vi-datetime.js
File metadata and controls
186 lines (133 loc) · 4.59 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* @Author: Vinod Selvin
* @Description: Simple Date plugin
*/
$(function () {
$('.vi-date').wrap('<div class="wrap-date"></div>');
var formats = ['dd-mm-yyyy', 'yyyy-mm-dd', 'mm-dd-yyyy', 'dd-yyyy-mm', 'yyyy-dd-mm', 'mm-yyyy-dd',
'mm-yyyy', 'yyyy-mm',
'dd-yyyy', 'yyyy-dd',
'mm-dd', 'dd-mm',
'yyyy', 'dd', 'mm'];
var tgday = '<select class="vdays"></select>';
var tgmonth = '<select class="vmonths"></select>';
var tgyears = '<select class="vyears"></select>';
for (var i = 0; i < formats.length; i++) {
var format = formats[i];
var final_format = '.vi-date[data-vi-format="' + format + '"]';
var fgdate = '';
var exp_format = format.split("-");
var len = exp_format.length;
for(var j=0; j<= len-1; j++){
if (exp_format[j] == 'dd') {
fgdate += tgday;
}
else
if (exp_format[j] == 'mm') {
fgdate += tgmonth;
}
else
if (exp_format[j] == 'yyyy') {
fgdate += tgyears;
}
}
$(final_format).hide();
$(final_format).after(fgdate);
populateYears(format);
populateMonths(format);
updateNumberOfDays();
}
});
/*
* @Author: Vinod Selvin
* @Description: populate years select box
* @params: format-> Date format
*/
function populateYears(format) {
var final_format = '.vi-date[data-vi-format="' + format + '"]';
$(final_format).parent().find('.vyears').append($('<option />').val(0).html('yyyy'));
for (i = new Date().getFullYear(); i > 1900; i--) {
$(final_format).parent().find('.vyears').append($('<option />').val(i).html(i));
}
}
/*
* @Author: Vinod Selvin
* @Description: populate months select box
* @params: format-> Date format
*/
function populateMonths(format) {
var final_format = '.vi-date[data-vi-format="' + format + '"]';
$(final_format).parent().find('.vmonths').append($('<option />').val(0).html('mm'));
for (i = 1; i < 13; i++) {
$(final_format).parent().find('.vmonths').append($('<option />').val(i).html(i));
}
}
/*
* @Author: Vinod Selvin
* @Description: function to update the days
*/
function updateNumberOfDays() {
$('.vdays').html('');
month = $('.vmonths').val();
year = $('.vyears').val();
days = daysInMonth(month, year);
$('.vdays').append($('<option />').val(0).html('dd'));
for (i = 1; i < days + 1; i++) {
$('.vdays').append($('<option />').val(i).html(i));
}
}
/*
* @Author: Vinod Selvin
* @Description: helper function
*/
function daysInMonth(month, year) {
return new Date(year, month, 0).getDate();
}
$(document).ready(function () {
$('.vi-date').nextAll('select').on('change', function (e) {
var pval = $(this).prevAll('.vi-date').val();
var cval = $(this).val();
var dclass = $(this).attr('class');
var format = $(this).prevAll('.vi-date').data('vi-format');
var inp_tag = $(this).prevAll('.vi-date');
var final_date = processDate(pval, cval, dclass, format);
inp_tag.val(final_date);
});
});
/*
* @Author: Vinod Selvin
* @Description: Processing Date to require format
* @Params: pval-> Previous Input in textbox, cval-> current value from select field, dclass-> Class of select field, format-> Date format.
*/
function processDate(pval, cval, dclass, format) {
var final_date = format;
var map_date = {'vmonths': 'mm', 'vdays': 'dd', 'vyears': 'yyyy'};
$.each(map_date, function (key, value) {
if (key == dclass) {
if (pval != '') {
var arr = format.split('-');
var arr2 = pval.split('-');
var decis = value;
$.each(arr, function (key1, value1) {
if (value1 == value) {
if (arr2[key1] == arr2[key1 - 1]) {
decis = "-" + arr2[key1 - 1];
cval = "-" + cval;
final_date = pval.replace(decis, cval);
} else {
decis = arr2[key1];
final_date = pval.replace(decis, cval);
}
}
});
} else {
final_date = final_date.replace(value, cval);
}
}
});
return final_date;
}
/*
* @Author: Vinod Selvin
* @Description: Simple Date plugin (END OF SCRIPT)
*/