Autumn Posted August 16 Share Posted August 16 Hello, I'm trying to format a date to 08/31/2024 from 20240831. I tried using the rule "Format Date" but the input data is not able to be converted. I'm pretty new to coding like this and have no clue what else to try. The error that I am getting is: uncaught exception: Error in fuction "DataFromString": could not convert "20240831" to a valid date. This is the coding that it shows in Java: // Rule converted from XML Template "Format Date: Registration End Date": // Begin XML Template selections // var dateField = "Registration End Date"; // "Choose the date field:" (Required): FieldOrRuleList var format = "MM/dd/yyyy"; // "Choose the format:" (Required): PickList ["M/d" (3/14), "M/d/yy" (3/14/01), "M/d/yyyy" (3/14/2001), "MM/dd/yy" (03/14/01), "MM/dd/yyyy" (03/14/2001), "yyyy/M/d" (2001/3/14), "d/M" (14/3), "d/M/yy" (14/3/01), <...11 others...>] var useDashes = false; // "Use dashes (-) instead of slashes (/)": CheckBox var includeWeekday = false; // "Prepend weekday name": CheckBox var language = "English"; // "Language (for day and month names)": Language // End XML Template selections // var theDate = DateFromString(RuleOrField(dateField)); if (useDashes) { format = ReplaceSubstring(format, "/", "-"); } var isAsian = (language.indexOf("Chinese") == 0 || language == "Japanese"); var AsianFormats = { "MMM d" : "M月d日", "yyyy MMM d" : "yyyy年M月d日", "yy MMM d" : "yy年M月d日", "MMM yyyy" : "yyyy年M月", }; if (isAsian) format = AsianFormats[format] || format; if (includeWeekday) { var delim = ""; if (format.indexOf(',') > -1) delim = ','; if (isAsian) format += delim + "EE"; else format = "EE" + delim + " " + format; } return FormatDate(theDate, format, language); Quote Link to comment Share on other sites More sharing options...
Douglas Cogan Posted August 16 Share Posted August 16 If you know that the date will always be yyyymmdd then this code will work: var date = "20240831"; // var date = Field("Date"); return Mid(date, 5,2) + "/" + Right(date,2) + "/" + Left(date,4); In line 1, I assign the variable date to the literal value for you to test, but if this works you can uncomment the second line and change it to the field that the date comes from. 1 Quote Link to comment Share on other sites More sharing options...
Alex Marshall Posted August 16 Share Posted August 16 Hello, Try the following code: MyDateAsString = "20240831"// or Field name used var MyDateYear = Int(Mid(MyDateAsString, 1, 4)); var MyDateMonth = Int(Mid(MyDateAsString, 5, 2)); var MyDateDay = Int(Mid(MyDateAsString, 7, 2)); var MyDate = new Date(MyDateYear, MyDateMonth-1, MyDateDay); return FormatDate(MyDate, "mm/dd/yyyy"); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.