Date Add in Javascript (Tips & Trick)
September 1, 2007
Tuesday, August 7th in Programming | No comments
I getting bored to find to know of how we could to adding the date in javascript. I’m looking around for this function but I couldn’t found one just because my damn low speed internet connection,what a shame, but I simply make it on my own. here’s the code
var oneMinute = 60 * 1000
var oneHour = oneMinute * 60
var oneDay = oneHour * 24
function dateAdd(vtype,vnumber,vdate){
if(vtype==”m”) {
var newDate = addMonth(vnumber,vdate);
}else if(vtype==”d”){
var newDate = addDay(vnumber,vdate)
}
return newDate
}
function addDay(vnum,vdate){
var objTanggal=vdate.split(’/');
var tgl=objTanggal[1];
var bln=objTanggal[0]-1;
var thn=objTanggal[2];
var targetDate=new Date(thn,bln,tgl);
var numofins = oneDay * vnum
var dateInMs = targetDate.getTime()
dateInMs += numofins
targetDate.setTime(dateInMs)
return targetDate;
}
Just simply use this function dateAdd – i took the name same as the dateAdd function in Coldfusion. first parameter would be type of date element in which will added from, it could be “m” for month or “d” for day, but I have no much time so I just made for “d”. Next is the second paramater, it would be the number that we want to add. and the last parameters, will be the initial value of date which we want to add. Well, this code is still under development, and still has many bugs, incompatibility and unstable. I don’t responsible if you got complaint from your client because u use this code… hahahahaha
My Ajax Engine
April 17, 2007
Ajax = {};
Ajax.makeRequest = function(method, url, callbackMethod)
{
this.ajaxRequest = (window.XMLHttpRequest)? new XMLHttpRequest(): new ActiveXObject(“Msxml2.XMLHTTP”);
this.ajaxRequest.onreadystatechange = callbackMethod;
this.ajaxRequest.open(method, url, true);
this.ajaxRequest.send(url);
//alert(“Content-Type : ” + this.ajaxRequest.getResponseHeader(‘Content-Type’));
//alert(“rxml: “+this.ajaxRequest.responseXML);
//alert(“rtext: “+this.ajaxRequest.responseText);
//alert (this.request.responseXML.documentElement);
}
Ajax.checkReadyState = function(_id)
{
//alert(document.getElementById(_id));
switch(this.ajaxRequest.readyState)
{
case 1:
document.getElementById(_id).innerHTML = ‘Saving …’;
break;
case 2:
document.getElementById(_id).innerHTML = ‘Saving …’;
break;
case 3:
document.getElementById(_id).innerHTML = ‘Saving …’;
break;
case 4:
AjaxUpdater.isUpdating = false;
document.getElementById(_id).innerHTML = ”;
alert(HTTP.status(this.ajaxRequest.status));
return HTTP.status(this.ajaxRequest.status);
default:
document.getElementById(_id).innerHTML = “An unexpected error has occurred.”;
}
}
Ajax.getResponse = function()
{
if(this.ajaxRequest.getResponseHeader(‘Content-Type’).indexOf(‘xml’) != -1)
{
return this.ajaxRequest.responseXML.documentElement;
}
else
{
return this.ajaxRequest.responseText;
}
}
AjaxUpdater = {};
AjaxUpdater.initialize = function()
{
AjaxUpdater.isUpdating = false;
}
AjaxUpdater.initialize();
AjaxUpdater.Update = function(method,service,callback)
{
if(callback == undefined || callback == “”) { callback = AjaxUpdater.onResponse; }
Ajax.makeRequest(method, service, callback);
AjaxUpdater.isUpdating = true;
}
AjaxUpdater.onResponse = function()
{
if(Ajax.checkReadyState(‘loading’) == 200)
{
AjaxUpdater.isUpdating = false;
}
}