How to convert epoch unix timestamp (in milliseconds) to date in YYYYMMDD

One of the fields in the payload to workflow is a date field and it is in format of unix timestamp (in milliseconds). I want to find a way to convert it to a date string in format of YYYYMMDD. I tried the date functions in workflow transform however not lucky.

Does anyone have any idea? Thanks!

Hi,

You can try Java Service

long unix_seconds = 1372339860;
   //convert seconds to milliseconds
   Date date = new Date(unix_seconds*1000L); 
   // format of the date
   SimpleDateFormat jdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
   jdf.setTimeZone(TimeZone.getTimeZone("GMT-4"));
   String java_date = jdf.format(date);

Hey Hongjie

I think this is a workflow related ask.
There’s a few ways to do this :slight_smile:

  1. EpochDate Time is milliseconds since 1970, therefore you can use a transformer to increment a date starting from 1970-01-01 and add the milliseconds, like below, but passing the epoch time rather than hardcoding like I did in the screenshot

You can also take into account locales/timezones and specify an output format:
image

  1. Write a node.js action (instead of a CLI connector) and put in code something like this:
var request = require ("request");

module.exports = function(){

    this.id = "my-first-action"; 
    this.label = "Sample"; 
    this.help = "Sample code for reference"; 
 
    this.input = {
    "title": "From Epoch",
    "type": "object",
    "properties": {
      "epochDateTime": {
        "title": 'epochDateTime',
        "type": 'integer',
        "description": 'epoch/unix date/time',
        "minLength": 1
      },

    }
  },

  this.output = {
    "title": "output",
        "type": "object",
        "properties": {
      "isoDatetime": {
        "title": 'iso Date/Time',
        "type": 'string',
        "description": 'date time in ISO format',
        "minLength": 0
      },
    }
  },

    this.execute = function(input,output){
        if(input.epochDateTime){
            var date = new Date(input.epochDateTime);
            return output(null, { "isoDateTime" : date.toISOString()});
        }
    } 
}
  1. Write the same but as a CLI connector.
  2. You can do similar as option 1 in a Flow Service.

Clearly the first option is the most simple :slight_smile:

If you ever need to go the opposite direction then CalculateDateDifference can be used, to calculate the number of seconds between 1970-01-01 and the datetime, then multiply up to get the milliseconds value with another transformer.

Some people might find this easier in a FlowService so just for completeness, I attached a couple of FlowService samples exported from wm.io integration to convert an Epoch date to Excel formatted date (the excel format date always blows my mind :exploding_head:), and also a convert date time to Epoch.

Just remember sometime epoch has two flavours to content with just to make life difficult - seconds or milliseconds :smiley:

convertEpochDateToExcel.zip (8.4 KB)
dateTimeToEpoch.zip (8.3 KB)

2 Likes

Hey @srikanth.prathipati1803 … it’s been a long time :slight_smile:

Currently Java Services aren’t avail in webmethods.io (hint: we are working on this though)
You can achieve this in FlowService even without a Java Service though with some creative use of the date functions :brain:

Thank you Dave. The first option works well in workflow transform. I will use this method at the moment.

1 Like

You are very welcome Hongjie!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.