Solved

Custome DateTime Format

  • 5 January 2023
  • 8 replies
  • 334 views

Userlevel 3
Badge +8

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

8 replies

Userlevel 6
Badge +16

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 3
Badge +8

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 6
Badge +16

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 4
Badge +10

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 3
Badge +8

Thanks @Garrett 

Badge +1

@Garrett  Thank you for providing the regex solution above. Can you help me to convert format “dddd, dd MMM YYYY” to US format “dddd, MMM dd YYYY” please? I cannot figure out the pattern and replacement text if I want “Monday, 22 May 2023” to display as “Monday, May 22, 2023”. 

Userlevel 6
Badge +16

Hi @jleung 

Here you go

Pattern:
(\w+,) (\d{2}) (\w+) (\d{4})

Replacement Text:
$1 $3 $2, $4

 

Testing

Date     : Sunday, 01 Jan 2023
Formatted: Sunday, Jan 01, 2023

Date     : Wednesday, 18 Jan 2023
Formatted: Wednesday, Jan 18, 2023

Date     : Friday, 28 Apr 2023
Formatted: Friday, Apr 28, 2023

Date     : Monday, 22 May 2023
Formatted: Monday, May 22, 2023

 

Badge +1

@Garrett  this works really nice! Thanks so much for your help!

Reply