Correct for 2-digit date entry in Javascript

In working with the function described in the last post, I realized that I needed some way to correct the year when a user entered a 2-digit year as opposed to a 4-digit year. This is because Javascript interprets ‘3/1/09’ as ‘March 1, 1909’ — throwing off my calculations quite alarmingly. There are a variety of fixes for this posted online, but most of them are focused on 19xx and 20xx. And even though it seems unlikely that anyone will be using anything I’ve written in 21xx, I still wanted to plan for it – wanted to learn from my y2k forebears.

So I worked out a script that subtracts the first two numbers of the entered year from the first two numbers of the current one, multiplies the result by 100 and then adds it to the entered year. Thus, if the current year is 2008 and the entered year is 1909, the script does this:

  • 20 – 19 = 1
  • 1 * 100 = 100
  • 1909 + 100 = 2009

If the current year is 2008 and the entered year is 2009, the script does this:

  • 20 – 20 = 0
  • 0 * 100 = 0
  • 2009 + 0 = 2009

And, if someone is still using my script in 2108, and javascript still interprets a 2-digit year of ’09 as 1909, the script would work like this:

  • 21 – 19 = 2
  • 2 * 100 = 200
  • 1909 + 200 = 2109

Here’s the script:

function correctYear(dt)
{
//get current date
var currDate = new Date();

//get full year for current date and arg date and convert to string
var dtYear = dt.getFullYear() + “”;
var currYear = currDate.getFullYear() + “”;

//subtract first two numbers of arg year from first two numbers of current year
var diff = (Number(currYear.substring(0,2)) – Number(dtYear.substring(0,2)));

//multiply difference by 100 and add to arg year
dtYear = Number(dtYear) + (diff * 100);

//set corrected year as arg date year, and return corrected date object
dt.setFullYear(dtYear);
return dt;
}

It’s not perfect – if the someone in 2108 really meant 2009, not 2109, then they’ll have to type that out. Same for someone in 2098 referring to 2102 – if they type in ’02, my script will interpret that as 2002 (of course, once the century turns, it’s all good again). But I took my shot. You can see the function in action in my last post.

Advertisements

Get the difference between two dates with Javascript

This is nothing earthshaking; in fact, it’s almost a complete rip off of another blog post, where I initially found what I needed. But I thought I’d put it here, so it would be easier for me to find it again when I need it again:-)

function daysDiff(dayStart, daySubtract)
{

//if either date is empty, return empty string
if ((dayStart == ”||dayStart == null)||(daySubtract == ”||daySubtract == null)){
return ”;
}
else {
//convert values to dates
var dateStart = new Date(dayStart);
var dateSubtract = new Date(daySubtract);

//correct for 2-digit year entry
var dateStart = correctYear(dateStart);
var dateSubtract = correctYear(dateSubtract);

//one day in milliseconds
var oneDay=1000*60*60*24;

//use the getTime() method of the javascript date object, which converts
//date to milliseconds. Subtract dates, then divide by oneDay variable
//to convert milliseconds to days. Round using the Math.round()* method.
var days = Math.round((dateStart.getTime()-dateSubtract.getTime())/(oneDay));

//return variable
return days;
}
}

*Originally, I used Math.ceil() here (because the blog I ripped off used Math.ceil).  However, I found in practice that Math.ceil did not give me the results I wanted.  Math.ceil always rounds upwards to the nearest integer, whereas Math.round will round up or down to the nearest integer.

Reporting Services – Report Manager won’t work after renaming web server

It took me a while to find out where I had to make a change when one of our clients changed the name of their web server. The error message I got when I tried to browse the Report Manager from IIS said “The remote name could not be resolved:” and displayed the old name of the web server.

There is a config file called RSWebApplication.config. You will find this file in your Reports Virtual Directory. The default location for this file on my development machine is C:Program FilesMicrosoft SQL ServerMSSQL.3Reporting ServicesReportManager.

In this file, you need to change the value of the ReportServerURL to reflect the new name of your web server.