Tag Archives: SPUtility.DateParse

SPUtility.DateParse !♥ seconds: SPUtility.DateParse removes the time component if you parse a date that incudes seconds!??!

Okay here’s another baffling API bug:

If you parse a date using SPUtility.ParseDate(web, “11/29/2009 13:37:12”, SPDateFormat.DateTime, false) it will return you a date object without parsing the time!

Remove the seconds SPUtility.ParseDate(web, “11/29/2009 13:37”, SPDateFormat.DateTime, false) and we are good to go. Full program listing of bug follows:

using System;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

public class SPUtilityParseDateTest
{
	public static void RunSnippet()
	{
		using (SPSite site = new SPSite("http://localhost"))
		{
			SPWeb web = site.RootWeb;			
			DateTime parsedDate = SPUtility.ParseDate(web, "11/29/2009 3:37:12 AM", SPDateFormat.DateTime, false);				
			WL(parsedDate.ToString());
				
			parsedDate = SPUtility.ParseDate(web, "11/29/2009 3:37 AM", SPDateFormat.DateTime, false);				
			WL(parsedDate.ToString());
			
		}
	}
		
	#region Helper methods
	
	public static void Main()
	{
		try
		{
			RunSnippet();
		}
		catch (Exception e)
		{
			string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
			Console.WriteLine(error);
		}
		finally
		{
			Console.Write("Press any key to continue...");
			Console.ReadKey();
		}
	}

	private static void WL(object text, params object[] args)
	{
		Console.WriteLine(text.ToString(), args);	
	}
	
	private static void RL()
	{
		Console.ReadLine();	
	}
	
	private static void Break() 
	{
		System.Diagnostics.Debugger.Break();
	}

	#endregion
}

The above code assumes you have US regional settings on your root web.

Advertisements
Tagged , , ,