Shamil Salakhetdinov
shamil at smsconsulting.spb.ru
Wed Oct 21 17:47:14 CDT 2009
Hi Robert, Just a quick & dirty code snippet - it's C# but simulates VBA as close as possible: using System; using VBA = Microsoft.VisualBasic.Strings; namespace TestConsole.App { public class DateConvertor { public static DateTime Convert(string dateStr) { string temp = dateStr; string monthStr; string dayStr; string yearStr; DateTime retValue = DateTime.MinValue; if (VBA.Left(temp, 1) == "1" || VBA.Left(temp, 1) == "0") { // two digits monthNum monthStr = VBA.Left(temp, 2); temp = VBA.Mid(temp, 3); } else { // one digits monthNum monthStr = VBA.Left(temp, 1); temp = VBA.Mid(temp, 2); } if (VBA.Len(temp) < 2) throw new ApplicationException("Invalid date format: " + dateStr); else if (VBA.Len(temp) == 2) { temp = "00" + temp; } else if ( (VBA.Left(temp, 1) == "/") || (VBA.Left(temp, 1) == "-") ) { temp = VBA.Mid(temp, 2); } dayStr = VBA.Left(temp, 2); temp = VBA.Mid(temp, 3); if ( (VBA.Left(temp, 1) == "/") || (VBA.Left(temp, 1) == "-") ) temp = VBA.Mid(temp, 2); if (VBA.Len(temp) == 1) temp = "200" + temp; else if (VBA.Len(temp) == 2) temp = "20" + temp; yearStr = temp; retValue = (dayStr != "00") ? Microsoft.VisualBasic.DateAndTime.DateSerial( Int32.Parse(yearStr), Int32.Parse(monthStr), Int32.Parse(dayStr)) : Microsoft.VisualBasic.DateAndTime.DateSerial( Int32.Parse(yearStr), Int32.Parse(monthStr) + 1, 0); return retValue; } public static string[] TestDates { get { return new string[] { "909", "0909", "09/21/09", "09-21-09", "092109", "92109", "9212009" }; } } public static void Test() { Console.WriteLine("// Test Output:"); Console.WriteLine("// ------------"); foreach (string date in TestDates) { Console.WriteLine("// {0} => {1:MM/dd/yyyy}", date, Convert(date)); } // Test Output: // ------------ // 909 => 09/30/2009 // 0909 => 09/30/2009 // 09/21/09 => 09/21/2009 // 09-21-09 => 09/21/2009 // 092109 => 09/21/2009 // 92109 => 09/21/2009 // 9212009 => 09/21/2009 } } } HTH. -- Shamil -----Original Message----- From: accessd-bounces at databaseadvisors.com [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Robert Sent: Wednesday, October 21, 2009 10:17 PM To: 'Access Developers discussion and problem solving' Subject: [AccessD] All-In-One Date format Function: Hoooooowdy, Before re-inventing the wheel here, does anyone have or know of a function that can handle "free form" date formatting. I'm looking for a function that can parse any input and then format is correctly. Example of user inputs: 0909 09/21/09 09-21-09 092109 92109 9212009 etc. I don't want to use any formatting / Masking settings at the field or table level. I would like the user to enter it the way they want to (to the most degree possible)... WBR Robert <<< snip >>>