Parsing a date
by Adam Granger
JavaScript
var convertDateTime = function(dateTimeStr) {
var regex = /^(\d{2})\/(\d{2})\/(\d{4})\s(\d{2}):(\d{2})/;
var matches = regex.exec(dateTimeStr);
var returnValue = null;
console.log(matches);
var day = matches[1];
var month = matches[2] - 1;
if (matches != null) {
returnValue = new Date(matches[3],month, day, matches[4],matches[5]);
if (returnValue.getMonth() != month || returnValue.getDate() != day) {
returnValue = null; // bad day of month or month
}
}
return returnValue;
};
//console.log(convertDateTime('30/10/2013 07:99'));
function generate() {
var startDate = new Date(2013, 1, 20, 11, 29);
var endDate = new Date(2013, 1, 30, 9, 30);
var excludeWeekdays = false;
var excludeWeekends = true;
for (var date = startDate; date < endDate; date.setDate(date.getDate() + 1)) {
var weekday = date.getDay() >= 1 && date.getDay() <= 5;
var weekend = !weekday;
if (excludeWeekends && weekend || excludeWeekdays && weekday) {
continue;
}
var t0 = new Date(date.getFullYear(), date.getMonth(), date.getDate(), startDate.getHours(), startDate.getMinutes());
var t1 = new Date(date.getFullYear(), date.getMonth(), date.getDate(), endDate.getHours(), endDate.getMinutes());
console.log(t0 + " " + t1);
}
}
generate();