Convert a date object to a string in JavaScript

If you want to convert a date object to a string in JavaScript and you use jQuery, then you can use the function $.datepicker.formatDate.

 

The following QUnit test will succeed:

/// <reference path="Scripts/qunit.js"/>
/// <reference path="Scripts/jquery-1.8.3.js"/>
/// <reference path="Scripts/jquery-ui-1.9.2.js"/>
"use strict";
test('JsTest', function ()
{
var input = new Date(2012, 10, 28); // Note: months start at zero!
var expected = '2012-11-28';
var actual = $.datepicker.formatDate('yy-mm-dd', input);
ok(expected == actual,
'Dates are not equal. Expected:' +
expected + ' _ Actual:' + actual);
});

Convert string to date in JavaScript

If you want to convert a string to a date object in JavaScript and you use jQuery, then you can use the function $.datepicker.parseDate.

 

The following QUnit test will succeed:

/// <reference path="Scripts/qunit.js"/>
/// <reference path="Scripts/jquery-1.8.3.js"/>
/// <reference path="Scripts/jquery-ui-1.9.2.js"/>
"use strict";
test('JsTest', function ()
{
var text = '2012-11-28';
var expected = new Date(2012, 10, 28); // Note: months start at zero!
var actual = $.datepicker.parseDate('yy-mm-dd', text);
ok(expected.getTime() == actual.getTime(),
'Dates are not equal. Expected:' +
expected.toISOString() + ' _ Actual:' + actual.toISOString());
});

Running javascript and typescript unit tests in Microsoft Visual Studio 2012 with Chutzpah

To unit test my JavaScript files in Microsoft Visual Studio 2012, side by side mine C# unit tests, I use Chutzpah.

Chutzpah comes with 2 Microsoft Visual Studio 2012 extensions:

1. Chutzpah Test Adapter for Visual Studio 2012, allows you to run QUnit / Jasmine javascript test files side by side C# unit tests in the test explorer or continuous build process.

2. Chutzpah – A JavaScript Test Runner, allows you to run individual QUnit / Jasmine JavaScript test files in Microsoft Visual Studio 2010 and 2012 by right clicking a test file and choosing "".

 

In Microsoft Visual Studio 2012

– Tools > Extensions and Updates…

– Install both plugins and restart Microsoft Visual Studio 2012

 

image

 

Create a new UnitTest project:

 

image

 

Enable "Run Tests after build"

(The "Play" button found at the top of the Test Explorer")

Now when you press CTRL + SHIFT + B the project will be build and all tests will execute.

 

image 

 

TIP: Never use CTRL + S to save your file, just use CTRL + SHIFT + B.

Now you can see the C# unit test "TestMethod1" succeeded.

 

QUnit for ASP.NET MVC

Right click unit test project and click Manage NuGet Packages…

Click install.

 

image

 

Add Code.js and Test.js

The Code.js file will contain the JavaScript code we want to test.

The Test.js  file will contain the JavaScript test code written, by using the QUnit test framework.

 

Code.js

image

 

Test.js

Reference qunit and the Code.js file to get code completion in your test code.

 

image

 

Code completion in JavaScript in Microsoft Visual Studio 2012 unit test file

 

image

 

 

Run C# and JavaScript unit tests side by side in the Test Explorer

By pressing CTRL + SHIFT + B

image

 

Run only JavaScript unit test file

If you only want to run the QUnit test, right click on the JavaScript Test.js file and choose "Run JS Tests".

 

image

 

If you want to run JavaScript unit tests by pressing a keyboard shortcut, you need to install

image

 

Then you can bind a shortcut to the ProjectAndSolutionsContextMenus.Project.RunJSTests command to run JS tests.

(http://matthewmanela.com/blog/chutzpah-1-1-0-released/)

 

image

Simple way for converting "Rijksdriehoek" coordinates to lat and long (WGS84) in C#

In the Netherlands "Rijksdriehoek" coordinates are used to show the exact position of an object on a chart. To convert "Rijksdriehoek" coordinates to lat and long coordinates in C# use the following class:

 

Note

This code is base on the information found in  http://www.dekoepel.nl/pdf/Transformatieformules.pdf

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Research.Core
{
public interface IRijksdriehoekComponent
{
string ConvertToLatLong(double x, double y);
}
public class RijksdriehoekComponent : IRijksdriehoekComponent
{
public string ConvertToLatLong(double x, double y)
{
string result = null;
// The city "Amsterfoort" is used as reference "Rijksdriehoek" coordinate.
int referenceRdX = 155000;
int referenceRdY = 463000;
double dX = (double)(x - referenceRdX) * (double)(Math.Pow(10,-5));
double dY = (double)(y - referenceRdY) * (double)(Math.Pow(10,-5));
double sumN = 
(3235.65389 * dY) + 
(-32.58297 * Math.Pow(dX, 2)) + 
(-0.2475 * Math.Pow(dY, 2)) + 
(-0.84978 * Math.Pow(dX, 2) * dY) + 
(-0.0655 * Math.Pow(dY, 3)) + 
(-0.01709 * Math.Pow(dX, 2) * Math.Pow(dY, 2)) + 
(-0.00738 * dX) + 
(0.0053 * Math.Pow(dX, 4)) + 
(-0.00039 * Math.Pow(dX, 2) * Math.Pow(dY, 3)) + 
(0.00033 * Math.Pow(dX, 4) * dY) + 
(-0.00012 * dX * dY);
double sumE = 
(5260.52916 * dX) + 
(105.94684 * dX * dY) + 
(2.45656 * dX * Math.Pow(dY, 2)) + 
(-0.81885 * Math.Pow(dX, 3)) + 
(0.05594 * dX * Math.Pow(dY, 3)) + 
(-0.05607 * Math.Pow(dX, 3) * dY) + 
(0.01199 * dY) + 
(-0.00256 * Math.Pow(dX, 3) * Math.Pow(dY, 2)) + 
(0.00128 * dX * Math.Pow(dY, 4)) + 
(0.00022 * Math.Pow(dY, 2)) + 
(-0.00022 * Math.Pow(dX, 2)) + 
(0.00026 * Math.Pow(dX, 5));
// The city "Amsterfoort" is used as reference "WGS84" coordinate.
double referenceWgs84X = 52.15517;
double referenceWgs84Y = 5.387206;
double latitude = referenceWgs84X + (sumN / 3600);
double longitude = referenceWgs84Y + (sumE / 3600);
// Input
// x = 122202
// y = 487250
//
// Result
// "52.372143838117, 4.90559760435224"
result = string.Format("{0}, {1}", 
latitude.ToString(CultureInfo.InvariantCulture.NumberFormat),
longitude.ToString(CultureInfo.InvariantCulture.NumberFormat));
return result;
}
}
}

Test

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Research.Core;
namespace UnitTest
{
[TestClass]
public class RijksdriehoekComponentTests
{
[TestMethod]
public void TestMethod1()
{
IRijksdriehoekComponent component = new RijksdriehoekComponent();
double x = 122202;
double y = 487250;
string result = component.ConvertToLatLong(x, y);
Assert.AreEqual("52.372143838117, 4.90559760435224", result);
}
}
}

How to start a process / application on an other computer from C#, by using PowerShell remoting

 

You van execute a process on an other machine from C#, by using the code below:

 

Example for calling the function

var rc = new RemotingComponent(null);
rc.RunRemoteProcess
(
@"MyMachine1", 
"MyMachine1\Administrator", 
"MyPassword1", 
@"C:\Windows\System32\notepad.exe",
string.Empty
);

C# class

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Security;
using System.Text;
namespace Research
{
public interface IRemotingComponent
{
void RunRemoteProcess(string machineName, string userName, string password, string processPath, string processArguments);
}
public class RemotingComponent : IRemotingComponent
{
public void RunRemoteProcess(string machineName, string userName, string password, string processPath, string processArguments)
{
var connectTo = new Uri(String.Format("http://{0}:5985/wsman", machineName));
_logger.Info("Building powershell command.");
var command = new StringBuilder();
command.AppendLine("$pinfo = New-Object System.Diagnostics.ProcessStartInfo");
command.AppendLine(string.Format(@"$pinfo.FileName = ""{0}""", processPath));
command.AppendLine("$pinfo.UseShellExecute = $false");
command.AppendLine("$pinfo.RedirectStandardError = $true");
command.AppendLine("$pinfo.RedirectStandardOutput = $true");
command.AppendLine("$pinfo.CreateNoWindow = $true");
command.AppendLine(string.Format(@"$pinfo.WorkingDirectory = ""{0}""", new FileInfo(processPath).Directory.FullName));
command.AppendLine(string.Format(@"$pinfo.Arguments = ""{0}""", processArguments));
command.AppendLine("$p = New-Object System.Diagnostics.Process");
command.AppendLine("$p.StartInfo = $pinfo");
command.AppendLine("$p.Start() | Out-Null");
command.AppendLine("$output = $p.StandardOutput.ReadToEnd()");
command.AppendLine("$p.WaitForExit()");
command.AppendLine("$output");
string shell = @"http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
using (var passing = new SecureString())
{
foreach (char c in password.ToCharArray())
{
passing.AppendChar(c);
}
passing.MakeReadOnly();
var credentials = new PSCredential(userName, passing);
var connection = new WSManConnectionInfo(connectTo, shell, credentials);
using (var runspace = RunspaceFactory.CreateRunspace(connection))
{
   runspace.Open();
using (var powershell = PowerShell.Create())
{
powershell.Runspace = runspace;
powershell.AddScript(command.ToString());
       var results = powershell.Invoke();
runspace.Close();
foreach (var obj in results.Where(o => o != null))
{
Console.Writeline(string.Format("Output: [{0}].", obj));
}
}
}
}
}
}
}

How to run tasks synchronously without blocking the UI thread in C# <= .NET 4.0

If you want to execute 3 tasks synchronously (one after the other), but you don’t want these tasks to block the UI thread. You can use the following code:

Note: Thread.Sleep(…) is a blocking operation and is used to mimic workload.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTests
{
[TestClass]
public class RliTests
{
public void DoWork1()
{
Console.WriteLine(
string.Format("DoWork1 started at [{0}] on Thread [{1}].", 
DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff"), 
Thread.CurrentThread.ManagedThreadId));
Thread.Sleep(1000);
Console.WriteLine(
string.Format("DoWork1 completed at [{0}] on Thread [{1}].", 
DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff"), 
Thread.CurrentThread.ManagedThreadId));
}
public void DoWork2()
{
Console.WriteLine(
string.Format("DoWork2 started at [{0}] on Thread [{1}].", 
DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff"), 
Thread.CurrentThread.ManagedThreadId));
Thread.Sleep(2000);
Console.WriteLine(
string.Format("DoWork2 completed at [{0}] on Thread [{1}].", 
DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff"), 
Thread.CurrentThread.ManagedThreadId));
}
public void DoWork3()
{
Console.WriteLine(
string.Format("DoWork3 started at [{0}] on Thread [{1}].", 
DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff"), 
Thread.CurrentThread.ManagedThreadId));
Thread.Sleep(3000);
Console.WriteLine(
string.Format("DoWork3 completed at [{0}] on Thread [{1}].", 
DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff"), 
Thread.CurrentThread.ManagedThreadId));
}
public void DoWorkOnUiTrhead()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(
string.Format("DoWorkOnUiTrhead started at [{0}] on Thread [{1}].", 
DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff"), 
Thread.CurrentThread.ManagedThreadId));
Thread.Sleep(1000);
Console.WriteLine(
string.Format("DoWorkOnUiTrhead completed at [{0}] on Thread [{1}].", 
DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff"), 
Thread.CurrentThread.ManagedThreadId));
}
}
/// <summary>
/// Executing a list of tasks synchronously, without blocking the UI.
/// </summary>
[TestMethod]
public void Test()
{
Console.WriteLine(
string.Format("UI ThreadId [{0}]",
Thread.CurrentThread.ManagedThreadId));
// Run task synchronously, but on an other thread, 
// so the UI thread is not blocked.
var taskAsync = new Task
(
RunTaskSyncrhonously, 
System.Threading.Tasks.TaskCreationOptions.LongRunning
);
taskAsync.Start();
// Execute work in the UI thread, to prove the UI is not blocked.
DoWorkOnUiTrhead();
}
public void RunTaskSyncrhonously()
{
var tasks = new List<Task>
{
new Task(DoWork1, 
System.Threading.Tasks.TaskCreationOptions.LongRunning),
new Task(DoWork2, 
System.Threading.Tasks.TaskCreationOptions.LongRunning),
new Task(DoWork3, 
System.Threading.Tasks.TaskCreationOptions.LongRunning)
};
foreach (Task task in tasks)
{
task.RunSynchronously(TaskScheduler.Default);
}
}
}
}

Output

As you can see in the output, the tasks are run on an other thread, so they don’t block the UI thread, but they are run synchronously (one after the other).

 
UI ThreadId [17]

DoWorkOnUiTrhead started at [2012-11-16 11:55:59.116] on Thread [17].

DoWork1 started at [2012-11-16 11:55:59.116] on Thread [18].

DoWorkOnUiTrhead completed at [2012-11-16 11:56:00.116] on Thread [17].

DoWorkOnUiTrhead started at [2012-11-16 11:56:00.116] on Thread [17].

DoWork1 completed at [2012-11-16 11:56:00.117] on Thread [18].

DoWork2 started at [2012-11-16 11:56:00.117] on Thread [18].

DoWorkOnUiTrhead completed at [2012-11-16 11:56:01.117] on Thread [17].

DoWorkOnUiTrhead started at [2012-11-16 11:56:01.117] on Thread [17].

DoWorkOnUiTrhead completed at [2012-11-16 11:56:02.117] on Thread [17].

DoWorkOnUiTrhead started at [2012-11-16 11:56:02.117] on Thread [17].

DoWork2 completed at [2012-11-16 11:56:02.118] on Thread [18].

DoWork3 started at [2012-11-16 11:56:02.118] on Thread [18].

DoWorkOnUiTrhead completed at [2012-11-16 11:56:03.118] on Thread [17].

DoWorkOnUiTrhead started at [2012-11-16 11:56:03.118] on Thread [17].

DoWorkOnUiTrhead completed at [2012-11-16 11:56:04.118] on Thread [17].

DoWorkOnUiTrhead started at [2012-11-16 11:56:04.118] on Thread [17].

DoWorkOnUiTrhead completed at [2012-11-16 11:56:05.118] on Thread [17].

DoWorkOnUiTrhead started at [2012-11-16 11:56:05.118] on Thread [17].

DoWork3 completed at [2012-11-16 11:56:05.119] on Thread [18].

DoWorkOnUiTrhead completed at [2012-11-16 11:56:06.119] on Thread [17].

DoWorkOnUiTrhead started at [2012-11-16 11:56:06.119] on Thread [17].

DoWorkOnUiTrhead completed at [2012-11-16 11:56:07.120] on Thread [17].

DoWorkOnUiTrhead started at [2012-11-16 11:56:07.120] on Thread [17].

DoWorkOnUiTrhead completed at [2012-11-16 11:56:08.120] on Thread [17].

DoWorkOnUiTrhead started at [2012-11-16 11:56:08.120] on Thread [17].

DoWorkOnUiTrhead completed at [2012-11-16 11:56:09.120] on Thread [17].

How to connect Microsoft Visio 2013 to a Microsoft SQL Server LocalDB instance

If you connect Microsoft Visio 2013 to a Microsoft SQL Server LocalDb instance by using the standard (localdb)\V11.0 instance name, you might get the error:

[DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied.

 

image

 

To fix this error use the following instance name: np:\\.\pipe\LOCALDB#92FE49CE\tsql\query

The number in bold can be found in the Microsoft SQL Server Management Studio 2012 Server Properties dialog, when connected to the (localdb)\V11.0 instance:

 

image

Change Windows 8 start screen color to black

Move your mouse to the right upper or right lower corner or swipe from the right.

Click on the wheel icon (lowest icon)

image

 

Click on Change PC settings

 

image

Click on Personalize.

Slide the slider to the one of the first four colors.

 

image

 

As you can see there are 21 background colors and around 18 fore ground colors.

In the beginning I thought, I could not change the Windows 8 start screen color to black because I focused on the little colored foreground squares, and they don’t contain the color black.