Solved

Custome DateTime Format

  • 5 January 2023
  • 5 replies
  • 43 views

Userlevel 2
Badge +6

Hi, 

 

Is there a way to use a custom date format in NWC (Im looking for an action that can do this). 
My date time format should be something like this: MM/DD/YYYY HH:MM:SS so it will display something like this: 02/04/2023 13:25:53 

I tried using the NWC Tools Format DateTime Action, but it just gives an error saying “The specified DateTime value has an unsupported DateTime pattern. [Error Code: TypeCasting.UnsupportedDateTimePattern]”. I also used convert a date to string, but there's no option for a custom format. Any suggestions?

icon

Best answer by Garrett 5 January 2023, 15:57

View original

5 replies

Userlevel 5
Badge +13

Hi @Prineel_V3 

First, use the “Format Date to String”. Store value into variable.

Next, apply Regex (Regular Expression) on that string to format into your required formatting  

Userlevel 2
Badge +6

Hi @Garrett ,

 

I did also attempt this, but there are no valid/usable operations to convert a datetime variable to a custom format (I could be doing something wrong though)

Userlevel 5
Badge +13

Hi @Prineel_V3 

Regex only works on text strings not datetime objects.

 

Here is the solution

Step 1: Using “Format Date to String”. Ensure the format has all the correct values (Year is YYYY, Month is MM). Month must be in correct value. MM = 01, MMM = Jan, MMMM = January. 

Step 2: Apply a regular expression on the output from Step 1.

Regex pattern is 

(\d{4})-(\d{2})-(\d{2})(.*)

Replacement text is

$2/$3/$1$4

Step 3: Output to Log to History

Output from the Log to History

 

Hope that helps

 

Userlevel 2
Badge +7

Sounds like a good enhancement request to me!  I can’t think of a good reason why the “Format date to string” action shouldn’t be able to take a custom format - just like the formatDate function in a form. 

 

As a side note, I did try to use an autocompleting form to convert a datetime variable to a custom format, but it looks like autocompleted forms don’t evaluate any variables within them.

Userlevel 2
Badge +6

Thanks @Garrett 

Reply