Jump to content

Formatting a date from 20240831 to MM/dd/yyyy


Autumn

Recommended Posts

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);
 

Link to comment
Share on other sites

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.

 

  • Like 1
Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...