Recreating the vbscript now() function in javascript

I have recently been revamping a feedback mechanism which was built using vbscript, seen as I work on an intranet we were using the now function to take a snapshot of the time that the user made an entry.

As usual the JavaScript equivalent doesn’t work exactly the same, I didn’t want to amend the database structure so instead I thought creating a JavaScript now() function shouldn’t be too difficult. I was right.

Line wraps marked >>

function now(){ var d = new Date(); var day = checker( d.getDate() ); var month = checker( d.getMonth() + 1 ); var year = d.getFullYear(); var hours = checker( d.getHours() ); var minutes = checker( d.getMinutes() ); var seconds = checker( d.getSeconds() ); var str = month + "/" + day + "/" + year + " " >> + hours + ":" + minutes + ":" + seconds; return str; }

Quite simple really, just set the variables and construct the string to look like the VBscript now().

Last thing is the checker function, all that does is add a 0 to the start of any number below 10.

function checker( val ){ if( val < 10 ){ return "0" + val; }else{ return val; }; }

Simple.

Posted on April 25, 2008 at 11:10 pm

With these tags: , ,

24 ways returns

With the return of 24 ways, I’m almost getting into the festive spirit, just maybe. I know for certain I am very much looking forward to the rest of 24 ways this year.

With three days already gone, be sure to add it to your rss feeds because I’m sure we still have 21 blindingly good articles to come!

At least I know when the family crack out that interactive DVD game, I will always have 24 ways to cure the bordem!

Posted on December 3, 2007 at 8:02 pm

With these tags: , , , ,

Post Tags

Subscribe

Here are my RSS feeds that you can subscribe to:

About Me

Hi! My names Damian Poole, I'm a huge web standards advocate and HTML ninja, when I'm not coding elegant web sites I like to procrastinate my time on world of warcraft.