Therefore I had a play with some reflection code, and have created a little command line application that can be used to see the available date formats output by the ddwrt utility.
Note that this code could be used to experiment with the output of some of the other ddwrt functions in the Microsoft.SharePoint.WebPartPages.DdwRuntime library - to see a list of the other functions, open up the Microsoft.SharePoint dll in Reflector.
One other tip - a list of the LCID values is available in this post on my blog
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
namespace DDWRT.FormatDate
{
class Program
{
static void Main(string[] args)
{
try
{
CultureInfo ci = new CultureInfo(1031);
Console.WriteLine(RunDDWRT("05-09-2007 11:00", ci));
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
public static string RunDDWRT(string szDate, CultureInfo ci)
{
string fullName = "Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c";
StringBuilder sb = new StringBuilder();
//Load the SharePoint Assembly
Assembly assem = Assembly.Load(fullName);
// Reference the DDWRT namespace
Type type = assem.GetType("Microsoft.SharePoint.WebPartPages.DdwRuntime", true, true);
//Find the sought method in DDWRT (the "FormatDateTime" method)
MethodInfo methodInfo = null;
MethodInfo[] methods = type.GetMethods();
foreach (MethodInfo method in methods)
{
//Uncomment the next line to see a list of all the methods available in the ddwrt class
//sb.AppendLine(string.Format(" {0}", method.Name));
if (method.Name == "FormatDate")
methodInfo = method;
}
if (methodInfo != null)
{
object objectInstance = Activator.CreateInstance(type);
sb.AppendLine("ddwrt.FormatDate Test");
sb.AppendFormat("\r\n");
sb.Append("Locale LCID:");
sb.AppendFormat("\t");
sb.Append(string.Format("{0} ({1})", ci.LCID.ToString(), ci.Name));
sb.AppendFormat("\r\n");
sb.Append("Date to format:");
sb.AppendFormat("\t");
sb.Append(szDate);
sb.AppendFormat("\r\n");
sb.AppendFormat("\r\n");
sb.Append("FormatFlag");
sb.AppendFormat("\t");
sb.Append("Formatted Date");
sb.AppendFormat("\r\n");
sb.AppendFormat("\r\n");
for (long formatFlag = 1; formatFlag < 16; formatFlag++)
{
try
{
string formattedDateTime = (string)methodInfo.Invoke(objectInstance, new Object[] { szDate, ci.LCID, formatFlag });
sb.Append(formatFlag);
sb.AppendFormat("\t\t");
sb.Append(formattedDateTime);
sb.AppendFormat("\r\n");
}
catch
{
sb.Append(formatFlag);
sb.AppendFormat("\t\t");
sb.Append("--------");
sb.AppendFormat("\r\n");
}
}
}
return sb.ToString();
}
}
}
No comments:
Post a Comment