A simple formatDate function for JavaScript

Written by the Great Rick Strahl at: http://west-wind.com/weblog/posts/282495.aspx

A simple formatDate function for JavaScript

One thing in JavaScript that’s really lacking is date formatting. If you want to display a date in JavaScript there aren’t a whole lot of options available especially if you want to display a date as numeric notation (ie. 10/31/2007 for example). I decided that I need at least some basic date formatting functionality in my apps, so I created a basic date formatting function. While I was at it I had to create a few helpers along the way which I’ll also show below.

This is one thing where the Microsoft ASP.NET AJAX client library actually excels – it has a whole complement of type formatting functions similar to .NET code, but since I’m not using the library I needed to roll my own. I took a brief look to see what the MS client libs are doing but the amount of code involved for formatting is huge and scattered all over the place. To be expected though given in the completeness of the support which is much more than my simple needs. (FWIW, it’s really interesting to see the sheer amount of code that runs in the MS AJAX libraries!).

I also searched around for a other JavaScript implementations and I dug up a few but the problem with many of them is that the code gets rather lengthy quickly as most libraries support too many features including string based date representations, which then requires localization etc.

My main goal is to provide only basic date formatting for numeric date display. Things like 12/01/1966 and 2005-12-31 19:00:31 and 12-07 or 12:01pm for example. That’s only a fraction of the date formatting we’re used to in .NET, but really how many different date formats do you ever work with anyway?

In any case here’s a formatDate extension for the Date prototype:

Date.prototype.formatDate = function(format)

{

var date = this;

if (!format)

format=”MM/dd/yyyy”;

var month = date.getMonth() + 1;

var year = date.getFullYear();

format = format.replace(“MM”,month.toString().padL(2,”0″));

if (format.indexOf(“yyyy”) > -1)

format = format.replace(“yyyy”,year.toString());

else if (format.indexOf(“yy”) > -1)

format = format.replace(“yy”,year.toString().substr(2,2));

format = format.replace(“dd”,date.getDate().toString().padL(2,”0″));

var hours = date.getHours();

if (format.indexOf(“t”) > -1)

{

if (hours > 11)

format = format.replace(“t”,”pm”)

else

format = format.replace(“t”,”am”)

}

if (format.indexOf(“HH”) > -1)

format = format.replace(“HH”,hours.toString().padL(2,”0″));

if (format.indexOf(“hh”) > -1) {

if (hours > 12) hours – 12;

if (hours == 0) hours = 12;

format = format.replace(“hh”,hours.toString().padL(2,”0″));

}

if (format.indexOf(“mm”) > -1)

format = format.replace(“mm”,date.getMinutes().toString().padL(2,”0″));

if (format.indexOf(“ss”) > -1)

format = format.replace(“ss”,date.getSeconds().toString().padL(2,”0″));

return format;

}

With this you can format dates like this:

var date = new Date();

var cr = “\n”;

var str = date.formatDate(“yyyy-MM-dd HH:mm”) + cr +

date.formatDate(“MM/dd/yyyy hh:mm t”) + cr +

date.formatDate(“MM-yyyy hh:mmt”) ;

alert(str);

The date formatting isn’t terribly flexible so months, days, hours, minutes and seconds can only be represented in 2 digit format, but again that’s the most common way to do it anyway around the world.

The code above depends on a few helper functions that String.prototype.padL and String.prototype.padR and String.repeat:

String.repeat = function(chr,count)

{

var str = “”;

for(var x=0;x

return str;

}

String.prototype.padL = function(width,pad)

{

if (!width ||width<1)

return this;

if (!pad) pad=” “;

var length = width – this.length

if (length < 1) return this.substr(0,width);

return (String.repeat(pad,length) + this).substr(0,width);

}

String.prototype.padR = function(width,pad)

{

if (!width || width<1)

return this;

if (!pad) pad=” “;

var length = width – this.length

if (length < 1) this.substr(0,width);

return (this + String.repeat(pad,length)).substr(0,width);

}

The static String.repeat repeats a given character or string n number of times, which is required for padding values. .padL and padR, pad a string left and right and also trim a string if the width is exceeded. This is useful in many situations for string trimming and in the date formatting for right filling numbers with 0′s (ie. 2 turns into 02).

While I was at it I also ran into a very basic string.format implemementation which was just too easy to pass up. Again, not a full implementation like in .NET in that it only supports string replacement without format specifiers, but still highly useful:

String.format = function(frmt,args)

{

for(var x=0; x

{

frmt = frmt.replace(“{” + x + “}”,arguments[x+1]);

}

return frmt;

}

With it you can turn the previous data example to:

var date = new Date();

var cr = “\n”;

alert(String.format(“Date 1: {0}\nDate 2:{1}\nDate3: {2}”,

date.formatDate(“yyyy-MM-dd HH:mm”),

date.formatDate(“MM/dd/yyyy hh:mm t”),

date.formatDate(“MM-yyyy hh:mmt”) )

);

Even this very basic implementation can save a fair bit of string concatenation code in many cases.

None of this is rocket science of course, but it’s nice to have a few small and lean functions that provide basic utility functionality to your JavaScript code. Basic date formatting especially is something I’ve often cursed and this will go a long way to simplifying matters.

Posted in javascript | 3 Comments

Where’s my .NET 3.5 (on IIS), Dude?

How .Net 3.5 runs on the .Net 2.0 compiler. Written by Rick Strahl at http://west-wind.com/WebLog/ShowPost.aspx?id=289139

Posted in asp.net | 4 Comments

String Formatting in C#

String Formatting in C#

Resource on formatting strings in =
C#:

http://blog.stevex.net/index.php/string-formatting-in-csha=
rp/

Posted in c# | 3 Comments

Be a Customer Service Ninja

From www.consumerist.com
Be a Customer Service Ninja

Inspired to by Mike D’s Vonage story, Austin writes in a hot tip for all of looking to pole vault low-level CSR and reach the Valhalla of customer service.

“Most all large companies have some sort of executive customer service staff, made up of individuals who have the power to cut through all sorts of red tape,” he writes. “The key is knowing how to access these wonderful people who can make things right when everything else has gone wrong.”

• For public companies, put the stock ticker symbol in Google Finance and pull up the profile page. The corporate office should be listed under Company Facts.
• Call the corporate office.
• Ask for a transfer to the office of the CEO.
• You will likely get an exec. assistant but that’s good. Voice mail is ok, too.
• Give succinct summary, including identifying details like order numbers and confirmation numbers.
• Remain nice.

“Within a day, you should get the phone call equivalent to the holy grail–a call back by someone on the executive service team.”

Using this method, Austin says he got Verizon to do in three days what it hadn’t in three months: install his DSL.

His full letter, after the jump…

Austin writes:

“In his email about Vonage, Mike D. actually mentions a very useful entity for those of us who do battle with various companies that attempt to screw us over. Most all large companies have some sort of executive customer service staff, made up of individuals who have the power to cut through all sorts of red tape. The key is knowing how to access these wonderful people who can make things right when everything else has gone wrong.

I find the easiest way to get your issue heard is to call the corporate offices and ask to be transferred to the office of the CEO, the assistant to the CEO, or some similar entity (reference the executive by name if you want to sound like you really know what you’re doing). To find the corporate phone number, a little basic sleuthing in Google is necessary, since the normal customer service number will likely either not know the phone number or not be willing to give it out. For publicly traded companies, just plug the ticker symbol into Google and pull up the Google Finance profile page–the phone number will usually be listed under “Company Facts.” Even if the company is not publicly traded, it usually isn’t difficult to find the phone number using Google–it may even be listed in some obscure corner of the company’s web site. Alternatively, you can also attempt to find the email address of the CEO or other executive, but I find that calling is often faster, because that makes them realize that you are so upset about something that you took the effort to find out who to call.

The operator will likely transfer you to an executive assistant, but this is exactly what we want. Sometimes it will be voice mail, but occasionally you’ll get a real live person. Either way, remember that you are dealing with busy people, so don’t bother rambling on about your problem, but rather try to give a succinct summary, including any identifying details that may be helpful (order numbers, confirmation numbers, etc.). As with many other things in life, remaining civil and calm will do wonders, particularly since all you need to do is get your foot in the door.

Within a day, you should get the phone call equivalent to the holy grail–a call back by someone on the executive service team. At this point, they may or may not have been able to access the complete details of what has already transpired, but now is a good time to fill them in. This person will be the one who will work with you until your problem is resolved, so if they don’t immediately offer it, be sure to get their phone number so you can contact them again should any other problems come up.

On a personal note, just a couple of months ago I had to make use of this strategy when Verizon decided that the DSL line I had ordered for my new apartment was clearly something I didn’t want, and customer service kept pushing back the scheduled installation date until it was over three months from when my lease started. Heck, once when I was on the phone with a normal customer service supervisor trying to see what was holding up my order, he rolled back my installation date by two weeks on the spot, but couldn’t give a reason why he needed to do so! At that point, I was so frustrated that I found the corporate switchboard phone number for Verizon, did exactly what I describe above, and am happy to say that within two and a half days I had a fully functioning DSL line in my new apartments, something that probably would have not happened for at least a month or two had I not contacted the CEO’s office directly.

I hope this tip proves helpful to everyone out there who may run into a brick wall known affectionately as customer service.

Posted in Financial | Leave a comment

Allow RDP to Work Through Kaspersky Firewall

Allow RDP to Work Through Kaspersky Firewall

Found this post on the Kaspersky Forums:


What you want to do is allow access to terminal =
server from anywhere. That’s :

protocol TCP. Source : anywhere / generic port. To destination : =
localhost on

port 3389.



To do that, edit rules for svchost.exe and add a new one with :

Allow inbound TCP connection where local port : 3389


Posted in c# | 1 Comment

156 Useful Run Commands

Good post from fixmyxp.com containing a list of Windows XP run commands: 156 Useful Run CommandsA lot of useful tips follow in the comments section.

To Access…. Run Command
Accessibility Controls access.cpl
Accessibility Wizard accwiz
Add Hardware Wizard hdwwiz.cpl
Add/Remove Programs appwiz.cpl
Administrative Tools control admintools
Adobe Acrobat (if installed) acrobat
Adobe Designer (if installed) acrodist
Adobe Distiller (if installed) acrodist
Adobe ImageReady (if installed) imageready
Adobe Photoshop (if installed) photoshop
Automatic Updates wuaucpl.cpl
Bluetooth Transfer Wizard fsquirt
Calculator calc
Certificate Manager certmgr.msc
Character Map charmap
Check Disk Utility chkdsk
Clipboard Viewer clipbrd
Command Prompt cmd
Component Services dcomcnfg
Computer Management compmgmt.msc
Control Panel control
Date and Time Properties timedate.cpl
DDE Shares ddeshare
Device Manager devmgmt.msc
Direct X Control Panel (If Installed)* directx.cpl
Direct X Troubleshooter dxdiag
Disk Cleanup Utility cleanmgr
Disk Defragment dfrg.msc
Disk Management diskmgmt.msc
Disk Partition Manager diskpart
Display Properties control desktop
Display Properties desk.cpl
Display Properties (w/Appearance Tab Preselected) control color
Dr. Watson System Troubleshooting Utility drwtsn32
Driver Verifier Utility verifier
Event Viewer eventvwr.msc
Files and Settings Transfer Tool migwiz
File Signature Verification Tool sigverif
Findfast findfast.cpl
Firefox (if installed) firefox
Folders Properties control folders
Fonts control fonts
Fonts Folder fonts
Free Cell Card Game freecell
Game Controllers joy.cpl
Group Policy Editor (XP Prof) gpedit.msc
Hearts Card Game mshearts
Help and Support helpctr
HyperTerminal hypertrm
Iexpress Wizard iexpress
Indexing Service ciadv.msc
Internet Connection Wizard icwconn1
Internet Explorer iexplore
Internet Properties inetcpl.cpl
Internet Setup Wizard inetwiz
IP Configuration (Display Connection Configuration) ipconfig /all
IP Configuration (Display DNS Cache Contents) ipconfig /displaydns
IP Configuration (Delete DNS Cache Contents) ipconfig /flushdns
IP Configuration (Release All Connections) ipconfig /release
IP Configuration (Renew All Connections) ipconfig /renew
IP Configuration (Refreshes DHCP & Re-Registers DNS) ipconfig /registerdns
IP Configuration (Display DHCP Class ID) ipconfig /showclassid
IP Configuration (Modifies DHCP Class ID) ipconfig /setclassid
Java Control Panel (If Installed) jpicpl32.cpl
Java Control Panel (If Installed) javaws
Keyboard Properties control keyboard
Local Security Settings secpol.msc
Local Users and Groups lusrmgr.msc
Logs You Out Of Windows logoff
Malicious Software Removal Tool mrt
Microsoft Access (if installed) access.cpl
Microsoft Chat winchat
Microsoft Excel (if installed) excel
Microsoft Frontpage (if installed) frontpg
Microsoft Movie Maker moviemk
Microsoft Paint mspaint
Microsoft Powerpoint (if installed) powerpnt
Microsoft Word (if installed) winword
Microsoft Syncronization Tool mobsync
Minesweeper Game winmine
Mouse Properties control mouse
Mouse Properties main.cpl
Nero (if installed) nero
Netmeeting conf
Network Connections control netconnections
Network Connections ncpa.cpl
Network Setup Wizard netsetup.cpl
Notepad notepad
Nview Desktop Manager (If Installed) nvtuicpl.cpl
Object Packager packager
ODBC Data Source Administrator odbccp32.cpl
On Screen Keyboard osk
Opens AC3 Filter (If Installed) ac3filter.cpl
Outlook Express msimn
Paint pbrush
Password Properties password.cpl
Performance Monitor perfmon.msc
Performance Monitor perfmon
Phone and Modem Options telephon.cpl
Phone Dialer dialer
Pinball Game pinball
Power Configuration powercfg.cpl
Printers and Faxes control printers
Printers Folder printers
Private Character Editor eudcedit
Quicktime (If Installed) QuickTime.cpl
Quicktime Player (if installed) quicktimeplayer
Real Player (if installed) realplay
Regional Settings intl.cpl
Registry Editor regedit
Registry Editor regedit32
Remote Access Phonebook rasphone
Remote Desktop mstsc
Removable Storage ntmsmgr.msc
Removable Storage Operator Requests ntmsoprq.msc
Resultant Set of Policy (XP Prof) rsop.msc
Scanners and Cameras sticpl.cpl
Scheduled Tasks control schedtasks
Security Center wscui.cpl
Services services.msc
Shared Folders fsmgmt.msc
Shuts Down Windows shutdown
Sounds and Audio mmsys.cpl
Spider Solitare Card Game spider
SQL Client Configuration cliconfg
System Configuration Editor sysedit
System Configuration Utility msconfig
System File Checker Utility (Scan Immediately) sfc /scannow
System File Checker Utility (Scan Once At Next Boot) sfc /scanonce
System File Checker Utility (Scan On Every Boot) sfc /scanboot
System File Checker Utility (Return to Default Setting) sfc /revert
System File Checker Utility (Purge File Cache) sfc /purgecache
System File Checker Utility (Set Cache Size to size x) sfc /cachesize=x
System Information msinfo32
System Properties sysdm.cpl
Task Manager taskmgr
TCP Tester tcptest
Telnet Client telnet
Tweak UI (if installed) tweakui
User Account Management nusrmgr.cpl
Utility Manager utilman
Windows Address Book wab
Windows Address Book Import Utility wabmig
Windows Backup Utility (if installed) ntbackup
Windows Explorer explorer
Windows Firewall firewall.cpl
Windows Magnifier magnify
Windows Management Infrastructure wmimgmt.msc
Windows Media Player wmplayer
Windows Messenger msmsgs
Windows Picture Import Wizard (need camera connected) wiaacmgr
Windows System Security Tool syskey
Windows Update Launches wupdmgr
Windows Version (to show which version of windows) winver
Windows XP Tour Wizard tourstart
Wordpad write

Posted in XP Tips | Leave a comment

How to configure SQL Server 2005 to allow remote connections

Microsoft Article 914277

INTRODUCTION
When you try to connect to an instance of Microsoft SQL Server 2005 from a remote computer, you may receive an error message. This problem may occur when you use any program to connect to SQL Server. For example, you receive the following error message when you use the SQLCMD utility to connect to SQL Server:
Sqlcmd: Error: Microsoft SQL Native Client: An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections.
This problem may occur when SQL Server 2005 is not configured to accept remote connections. By default, SQL Server 2005 Express Edition and SQL Server 2005 Developer Edition do not allow remote connections. To configure SQL Server 2005 to allow remote connections, complete all the following steps:
• Enable remote connections on the instance of SQL Server that you want to connect to from a remote computer.
• Turn on the SQL Server Browser service.
• Configure the firewall to allow network traffic that is related to SQL Server and to the SQL Server Browser service.
This article describes how to complete each of these steps.

Back to the top
MORE INFORMATION
To enable remote connections on the instance of SQL Server 2005 and to turn on the SQL Server Browser service, use the SQL Server 2005 Surface Area Configuration tool. The Surface Area Configuration tool is installed when you install SQL Server 2005.

Back to the top
Enable remote connections for SQL Server 2005 Express or SQL Server 2005 Developer Edition
You must enable remote connections for each instance of SQL Server 2005 that you want to connect to from a remote computer. To do this, follow these steps:
1. Click Start, point to Programs, point to Microsoft SQL Server 2005, point to Configuration Tools, and then click SQL Server Surface Area Configuration.
2. On the SQL Server 2005 Surface Area Configuration page, click Surface Area Configuration for Services and Connections.
3. On the Surface Area Configuration for Services and Connections page, expand Database Engine, click Remote Connections, click Local and remote connections, click the appropriate protocol to enable for your environment, and then click Apply.

Note Click OK when you receive the following message:
Changes to Connection Settings will not take effect until you restart the Database Engine service.
4. On the Surface Area Configuration for Services and Connections page, expand Database Engine, click Service, click Stop, wait until the MSSQLSERVER service stops, and then click Start to restart the MSSQLSERVER service.

Back to the top
Enable the SQL Server Browser service
If you are running SQL Server 2005 by using an instance name and you are not using a specific TCP/IP port number in your connection string, you must enable the SQL Server Browser service to allow for remote connections. For example, SQL Server 2005 Express is installed with a default instance name of Computer Name\SQLEXPRESS. You are only required to enable the SQL Server Browser service one time, regardless of how many instances of SQL Server 2005 you are running. To enable the SQL Server Browser service, follow these steps.

Important These steps may increase your security risk. These steps may also make your computer or your network more vulnerable to attack by malicious users or by malicious software such as viruses. We recommend the process that this article describes to enable programs to operate as they are designed to, or to implement specific program capabilities. Before you make these changes, we recommend that you evaluate the risks that are associated with implementing this process in your particular environment. If you choose to implement this process, take any appropriate additional steps to help protect your system. We recommend that you use this process only if you really require this process.
1. Click Start, point to Programs, point to Microsoft SQL Server 2005, point to Configuration Tools, and then click SQL Server Surface Area Configuration.
2. On the SQL Server 2005 Surface Area Configuration page, click Surface Area Configuration for Services and Connections.
3. On the Surface Area Configuration for Services and Connections page, click SQL Server Browser, click Automatic for Startup type, and then click Apply.

Note When you click the Automatic option, the SQL Server Browser service starts automatically every time that you start Microsoft Windows.
4. Click Start, and then click OK.
Note When you run the SQL Server Browser service on a computer, the computer displays the instance names and the connection information for each instance of SQL Server that is running on the computer. This risk can be reduced by not enabling the SQL Server Browser service and by connecting to the instance of SQL Server directly through an assigned TCP port. Connecting directly to an instance of SQL Server through a TCP port is beyond the scope of this article. For more information about the SQL Server Browser server and connecting to an instance of SQL Server, see the following topics in SQL Server Books Online:
• SQL Server Browser Service
• Connecting to the SQL Server Database Engine
• Client Network Configuration

Back to the top
Create exceptions in Windows Firewall
These steps apply to the version of Windows Firewall that is included in Windows XP Service Pack 2 (SP2) and in Windows Server 2003. If you are using a different firewall system, see your firewall documentation for more information.

If you are running a firewall on the computer that is running SQL Server 2005, external connections to SQL Server 2005 will be blocked unless SQL Server 2005 and the SQL Server Browser service can communicate through the firewall. You must create an exception for each instance of SQL Server 2005 that you want to accept remote connections and an exception for the SQL Server Browser service.

SQL Server 2005 uses an instance ID as part of the path when you install its program files. To create an exception for each instance of SQL Server, you must identify the correct instance ID. To obtain an instance ID, follow these steps:
1. Click Start, point to Programs, point to Microsoft SQL Server 2005, point to Configuration Tools, and then click SQL Server Configuration Manager.
2. In SQL Server Configuration Manager, click the SQL Server Browser service in the right pane, right-click the instance name in the main window, and then click Properties.
3. On the SQL Server Browser Properties page, click the Advanced tab, locate the instance ID in the property list, and then click OK.
To open Windows Firewall, click Start, click Run, type firewall.cpl, and then click OK.
Create an exception for SQL Server 2005 in Windows Firewall
To create an exception for SQL Server 2005 in Windows Firewall, follow these steps:
1. In Windows Firewall, click the Exceptions tab, and then click Add Program.
2. In the Add a Program window, click Browse.
3. Click the C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Binn\sqlservr.exe executable program, click Open, and then click OK.

Note The path may be different depending on where SQL Server 2005 is installed. MSSQL.1 is a placeholder for the instance ID that you obtained in step 3 of the previous procedure.
4. Repeat steps 1 through 3 for each instance of SQL Server 2005 that needs an exception.
Create an exception for the SQL Server Browser service in Windows Firewall
To create an exception for the SQL Server Browser service in Windows Firewall, follow these steps:
1. In Windows Firewall, click the Exceptions tab, and then click Add Program.
2. In the Add a Program window, click Browse.
3. Click the C:\Program Files\Microsoft SQL Server\90\Shared\sqlbrowser.exe executable program, click Open, and then click OK.

Note The path may be different depending on where SQL Server 2005 is installed.

Posted in asp.net, c#, SQL Server | Leave a comment

Add a Visual Studio Solution to a new SVN Repository

Add a Visual Studio Solution to a new SVN Repository

1. Navigate to master SVN directory =
(directory that stores all repositories)


2. Create new directory within master =
SVN directory


3. Right-Click on new directory and =
within SVN menu, choose Add New Repository


4. Open the Visual Studio =
solution


5. Open the solution explorer


6. Right click on the solution name =
and select "Force ANKH to load for this solution"


7. A popup will appear requesting the =
URL to the repository. Enter the location to the directory created above =
in the following style:
file:///D:/dir1/dir2/dir3

8. Click OK and the solution will be =
committed to the new repository

Posted in c# | 8 Comments

Visual Studio Could Not Write to Output File Annoyance

Visual Studio Could Not Write to Output File Annoyance

Posted By Steven Smith at aspadvice.com =
(
http://aspadvice.com/blogs/ssmith/archive/2005/03/21/1849.=
aspx
)

Every now and then my VS.NET solutions will fail =
to build because of an error like:

Could not write to outputfile (filepath in /obj/ =
folder).=A0 The file is being used by another process.

There is a KB article describing this =
bug:

BUG: Could Not Copy Temporary Files to =
the Output Directory

But to be honest, that has never helped me one =
bit.=A0 In my case, I’m not using a shared output folder, I am using =
Copy Local, and the project with the issues is only being referenced by =
Project References.=A0 So that KB is worthless, but it’s the only one =
I’ve found thus far.

Sometimes I get things to work by switching from =
Debug to Release mode, or vice versa.=A0 This usually works for one or =
two builds, sometimes more, but usually it ends up failing as well, at =
which point both the /obj/debug/assembly.dll and =
/obj/release/assembly.dll are being locked by VS.NET Intellisense and =
it’s game over.

Sometimes restarting VS.NET will help.=A0 Often =
not.=A0 Or, often only for one build.

What I’ve found worked for me most recently, and =
which I’m posting here as much for my own future reference as for =
anybody else, is this:

1) Find all the projects that reference the =
project whose assembly is causing the problem.

2) Remove all references to said project.

3) Build just that project.=A0 If it works, you’re good to go.

4) Re-add Project References to the project (for the ones you =
deleted).




Posted in c# | 10 Comments

Microsoft Alpha Filters

This site allows you to play around with the MS alpha filters and writes the CSS to accomplish the look chosen.

http://samples.msdn.microsoft.com/workshop/samples/author/filter/Alpha.htm

Posted in css, web | 1 Comment