Shamil Salakhetdinov
shamil at smsconsulting.spb.ru
Thu Oct 22 04:58:26 CDT 2009
Hi Max, No, I didn't. Did I note that was a "quick & dirty" code snippet, didn't I? (Somebody might even find it too "dirty"...) Please feel free to make fixes to cover your test cases as well as null or empty or excessively long input string as well as ... Your turn. Thank you. -- Shamil -----Original Message----- From: accessd-bounces at databaseadvisors.com [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Max Wanadoo Sent: Thursday, October 22, 2009 11:01 AM To: 'Access Developers discussion and problem solving' Subject: Re: [AccessD] All-In-One Date format Function: What about? 1. 9 <enter> - month 9 same year 2. 99 <enter> - month 9 year 2009. 3. 999 <enter> - day 9, month 9, year 2009 Have you put these through the code and see what they come out with? Max -----Original Message----- From: accessd-bounces at databaseadvisors.com [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Shamil Salakhetdinov Sent: 21 October 2009 23:47 To: 'Access Developers discussion and problem solving' Subject: Re: [AccessD] All-In-One Date format Function: 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 >>>