Recreating the vbscript now() function in javascript

April 25th, 2008

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 <p>Simple.</p>

Uncategorized | Comments | Trackback

Leave a Reply

  1.  
  2.  
  3.  
  4. XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
You can keep track of new comments to this post with the comments feed.

Search

About Me

Web Developer with amazingness knowledge of ExtJS, jQuery, PHP, ASP, SQL. Keen follower of F1 & Football and all things tech.

Now Playing

Survivors by Phil Collins
from the album Both Sides.
1 week ago via last.fm.

Recent Posts

Recent Comments

Categories

So Meta