I was facing problems with date time (with the different time zone) in JavaScript. We are setting Tehran time zone with daylight saving into Local machine. At that time, JavaScript date object was creating a problem.
Problem is: when user selects any date, that date is startdate or enddate of daylight and we are adding one day into selected date, then it will return us the same date. Many JQuery calender controls have the same issue:
Example:
var currentDate = new Date(2012,02,17);
currentDate = currentDate.setDate(parseInt(currentDate.getDate()) + parseInt(1));
alert(currentDate);
Solution of this problem: I created one function for add days:
function add(name, method) {
if (!Date.prototype[name]) {
Date.prototype[name] = method;
}
};
add("addDays", function (num) {
this.setDate(parseInt(this.getDate()) + parseInt(num));
return this;
});
function _daylightSavingAdjust(date) {
date.setHours(date.getHours() > 12 ? date.getHours() + 2 : 0);
return date;
}
var currentDate = new Date(2012,02,17);
currentDate = currentDate.addDays(1);
var currentDate = new Date(2012,02,17);
currentDate = _daylightSavingAdjust(currentDate.addDays(1));
You can do cool stuff on date with JavaScript.