Reverse two numbers without an additional variable

So I came across a post on StackOverflow today that intrigued me. It was a question on reversing two numeric values without the use of additional variable (i.e. temp, or what ever you’d like to call it). The code went as follows:

i = i + j; j = i - j; i = i - j;

Simple, right? Arguably not as simple to pick up on when you don’t know what to look for, but certainly alleviates an additional variable, which I am all for.

Just thought I’d pass this tiny little piece of code along.

Flattr this!

Convert Date to ISO Format

An instance came up on StackOverflow where a user wanted to convert a date stamp to the UTC facsimile, but in ISO format. So, with several other responses, and some playing around, I wanted to come up with a nice stream-line version. Here is what I came up with:

Number.prototype.pad = function(width,chr){
  1.  chr = chr || '0';
  2.  var result = this;
  3.  for (var a = 0; a < width; a++)
  4.  result = chr + result;
  5.  return result.slice(-width);
  6. }
  7. Date.prototype.toISOString = function(){
  8.  return this.getUTCFullYear().pad(4) + '-'
  9.  + (this.getUTCMonth()+1).pad(2) + '-'
  10.  + this.getUTCDay().pad(2) + 'T'
  11.  + this.getUTCHours().pad(2) + ':'
  12.  + this.getUTCMinutes().pad(2) + ':'
  13.  + this.getUTCSeconds().pad(2) + 'Z';
  14. }
  15. // usage
  16. var d = new Date;
  17. alert(d.toISOString());

It’s very simple, but in-line and convenient. This can be called directly off a date object, and returns the date in UTC YYYY-MM-DDTHH:MM:SSZ format (also known as ISO 8601 Format).

Flattr this!

Well, it’s officially up

I’ve conformed; I am officially a blogger.

I don’t know how this will all work honestly. I am anticipating using this for the purpose just posting tidbits of code I come across along the way. Basically, if I develop something I think others could use, if I find a piece of code that was hard to search for but is useful, or just general cool solutions to problems I come up with I’ll post it here.

In recent months I’ve been expanding my horizons and have come to know the MSDN Social site, the [in]famous StackOverflow website, and other personal blogs. This has been helpful in countless ways and I feel it’s my time to contribute.

So, with all that being said here is my blog. I hope you find something helpful on it, even if it’s a determination of what not to do. As always, feel free to comment on things I’ve posted (community involvement is a powerful tool).

Flattr this!