Sunday, January 30, 2011

Date Add in Javascript (Tips & Trick)

Hi Guys, Good day to you ! allow me repost a programming tips that I copied from my another blog. enjoy !


I came across from a problem where I found a little difficulty using the existing Javascript standard date functions. especially when I need to 'add' the date forward or backward. let say, for example, I want to know what is the date one month after 'now', or what is the date 7 days from now ?

I'm so familiar with Coldfusion where it provide dateAdd() function where you can use to add a date with a certain unit of time. Unfortunately, there's none as easy as this in Javascript.

Therefore I created my own Javascript functions that works similar with it. here is the code.

 //Simple Date Add Function
function dateAdd(datepart,number,objDate){
y = objDate.getFullYear();
m = objDate.getMonth()+1;
d = objDate.getDate();
hr = objDate.getHours();
mn = objDate.getMinutes();
sc = objDate.getSeconds();
newY=y;
newM=m;
newD=d;
if (datepart== 'y'){
newY = parseInt(y) + parseInt(number);
} else if (datepart== 'm') {
newM = parseInt(m) + parseInt(number);
} else {
newD = parseInt(d) + parseInt(number);
}
objNewDate = new Date(newY,newM,newD,hr,mn,sc);
return objNewDate;
}


This function took 3 parameters,

- Datepart. the option is "y" for year, "m" for month and "d" for day. currently this is the only options provided ^^, sure you can expand the usability on your own if you would so.


- Number. Number of units of datepart to add to date (positive, to get dates in the future; negative, to get dates in the past). Number must be an integer.


- objDate. Javascript Date/time object



Example :



function dateAddSample() {
example1 = dateAdd("m",3,new Date()); //add current date 3 months forward
example2 = dateAdd("m",-6,new Date()); //add current date 6 months backward
example3 = dateAdd("d",7,new Date()); //add current date 7 days forward
alert(example1);
alert(example2);
alert(example3);
}

That's all the code, hope it helpful. cheers !

Easy Block Facebook

Probably some of you have same problem with me, got a lot of important , but you are person who easily drown in distraction surfing in web sites such as facebook, twitter, youtube or else.

I got some tips you can use if you want to block those websites so you will unable to access it before you finish those important work. this tips can works in Windows without you need any additional application.

well, It simple as you edit a file named ‘hosts’ which placed in folder C:\Windows\System32\drivers\etc. here you go.

1. Open that hosts file using notepad, make sure you run the notepad as administrator. in Win7 you can right-click the Notepad’s icon in your start menu and click the run as administrator option

2. add the list of websites that you want to block. here is mine.

 
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 www.facebook.com
127.0.0.1 www.detik.com
127.0.0.1 www.twitter.com



3. Save it and done !.



it is easy right ?



This things works by redirect any of the listed domains and IPs to localhost 127.0.0.1. For example, if I access my Facebook using the browser, then my computer will do is automatically process those request and redirect me to specified IP which in this case is my localhost’s IP. this is very simple so that you don’t need any additional application to block or filter the websites.



That’s the tips from me, hope useful. keep productive !