Skip to content

Getting Useful Timestamp and QueryString in Javascript for .NET

March 28, 2012

Here is some really useful tips to collect a timestamp that is useful in the formatting aswell as collecting any querystring in Javascript for usage in .NET.

Firstly for the timestamp, you can simply call these Javascript commands: –

var timeStamp = new Date(x);

hostAddress= top.location.host.toString();
var date = new Number(timeStamp.getDate());
var month = new Number(timeStamp.getMonth());
var year = new Number(timeStamp.getFullYear());
url = “http://” + hostAddress + “/folder/viewdateinformation.aspx?date=” + date + “/” + (month + 1)+ “/” + year;
window.location = url;

The first line gets the timestamp, but the formatting is a very long time format, to collect the individual elements of the timestamp, you can call timeStamp.getDate() for the day, timeStamp.getMonth() for the month, and timeStamp.getFullYear() as the year in integers.

Be sure to add one to the month variable! It is indexed from 0.

You can then on the client side get .NET to redirect to a url by declaring a host Address using the line hostAddress= top.location.host.toString();

It is as simple as that!

Secondly for getting the QueryString, you can declare the following method in Javascript: –

function getParameterByName(name) {
var match = RegExp(‘[?&]’ + name + ‘=([^&]*)’)
.exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ‘ ‘));
}

This essentially uses regular expression and decodes the correct components from the URL in the windows box! Really useful for any QueryString!

You can then redirect the url in Javascript with the string from the QueryString in “information” appended to it! Useful for .NET to parse into the code behind.

url = “http://” + hostAddress + “/folder/viewdateinformation.aspx?date=” + date + “/” + (month + 1)+ “/” + year +”&count=” + y + “&information=” + getParameterByName(‘information’);

 

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: