timeformat in message template

I am trying to use a message template to set a header to a date that is in RFC 1123 format, e.g."Tue, 15 Feb 20222 10:01:45 GMT" . I looked at the timeformat function, but the examples on the Apigee doc are pretty simple ones. 

I've trying the following (nowInSeconds it is a string representing the number of seconds since epoch).

{timeFormatUTC('EEE, dd MMM yyyy HH:mm:ss zzz',nowInSeconds)} 

 

Any ideas? 

 

0 6 815
6 REPLIES 6

Hi,

 

If understand your questions correctly, you are looking for a way to format the date into a specific datetime format. I have not used timeformat function that you mentioned in the question, but there is another way to do it. That is by using crypto object in javascript policy.

the javascript function is this : 

crypto.dateFormat(format, [timezone], [time]).

You can look for this function here : https://docs.apigee.com/api-platform/reference/javascript-object-model#:~:text=The%20crypto%20object....

 

This particular object has more flexibility with date time format as it is based on https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

 

You can access your header flow variable inside javascript policy and format it.

Yes, like this:

 

<AssignMessage name='AM-1'>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
  <AssignVariable>
    <Name>timeformat1</Name>
    <Value>EEE, dd MMM yyyy HH:mm:ss zzz</Value>
    <!-- eg "Tue, 15 Feb 20222 10:01:45 GMT"  -->
  </AssignVariable>
  <AssignVariable>
    <Name>formattedTime</Name>
    <Template>{timeFormatUTCMs(timeformat1,system.timestamp)}</Template>
  </AssignVariable>
</AssignMessage>

 

Two notes

  • you don't need nowInSeconds, you can use system.timestamp. (Must also use timeFormatUTCMs because system.timestamp is in milliseconds)
  • The time format string itself needs to be defined in a variable, first
  • of course you can insert the template {timeFormatUTCMs...} anywhere that accepts a message template. It does not have to be within an AssignVariable.

Thank you @dchiesa1. I guess the problem was that I was trying to do it directly, without defining the time format in a variable.

 

<AssignVariable>
    <Name>formattedTime</Name>
    <Template>{timeFormatUTCMs('EEE dd MMM yyyy HH:mm:ss zzz',system.timestamp)}</Template>
  </AssignVariable>

 

 retested again and saw that this was actually the issue.

 

I've been using it before, but there I wasn't having any whitespaces in the timeformat. it was 'yyyyMMddhh:mm:ss'. 

 

Most probably related to this https://cloud.google.com/apigee/docs/api-platform/reference/message-template-intro#spacesnotallowed 

yes exactly. 

hi @dchiesa1 ,

I need time format like "YYYY-MM-DDTHH:MM:SS.SSSZ"

Ex: 2023-01-14T15:12:51.199Z

How to format the APIGEE generated timestamp to the required timestamp. I am using AssignMessage policy as shown.

You can use below to achieve the format - YYYY-MM-DDTHH:MM:SS.SSSZ

 

<AssignMessage name = 'AM-whatever'>
  <AssignVariable>
    <Name>timeformat</Name>
    <Value>yyyy-MM-dd'T'HH:mm:ss.SSS'Z'</Value>
  </AssignVariable>
  <AssignVariable>
    <Name>formattedTime</Name>
    <Template>{timeFormatUTCMs(timeformat,system.timestamp)}</Template>
  </AssignVariable>
  ...

 

 Good luck.