This post was originally published here

It is Friday, and just for fun and also because I would like to have a watch (one of my passions) in the cover picture, I decided to migrate a subset of an old project of mine: BizTalk MapperExtensions UtilityPack to Azure Functions, in this case, DateTime Conversion Functoids. You never know if they will be handy someday!

This project includes a list of Azure Functions that make data conversions easy to accomplish and that you can use anywhere, but most likely, I was thinking of using them inside Logic Apps if I will have a need to do this kind of task if they are not available out-of-the-box inside Logic Apps – and I know that some of them are not:

  • Convert from human-readable to epoch date function.
  • Convert from epoch to human readable date function.
  • And Convert date time format function.

Convert from human-readable to epoch date function.

This function allows you to convert a traditional date (Human Readable Date) into a Unix date (Epoch Date).

What is epoch time?

The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z). Literally speaking, the epoch is Unix time 0 (midnight 1-1-1970), but ‘epoch’ is often used as a synonym for ‘Unix time’. Many Unix systems store epoch dates as a signed 32-bit integer, which might cause problems on January 19, 2038 (known as the Year 2038 problem or Y2038).

The function receives a JSON payload with two properties:

  • The input Date or DateTime.
  • And a string describing the input format of the first parameter

Example:

{
    "datetime":"05-09-2012 14:00:00",
    "format":"dd-MM-yyyy HH:mm:ss"
}

The function’s output is a long value, representing the epoch date. For example: 1346850000

Convert from epoch to human readable date function.

This function allows you to convert a Unix date (Epoch Date) into a traditional date (Human Readable Date).

The function receives a JSON payload with one property:

  • A Unix date value.

Example:

{
    "unixtime": 1346850000
}

The function’s output is a date time in a string format: “2012-09-05T13:00:00”

And Convert date time format function.

Finally, this function allows you to convert the DateTime format into another DateTime format.

The function receives a JSON payload with three properties:

  • The input Date or DateTime.
  • A string describing the input format of the first parameter.
  • A string describing the output format of the first parameter.

Example:

{
    "inputDate": "05-09-2012 14:00:00",
    "inputFormat": "dd-MM-yyyy HH:mm:ss",
    "outputFormat": "yyyy-MM-dd HH:mm:ss"
}

The function’s output is a DateTime string according to the output format specified : “2012-09-05 14:00:00”

Where can I download it

You can download the complete Azure Functions source code here:

Hope you find this helpful! So, if you liked the content or found it helpful and want to help me write more content, you can buy (or help buy) my son a Star Wars Lego! 

Author: Sandro Pereira

Sandro Pereira lives in Portugal and works as a consultant at DevScope. In the past years, he has been working on implementing Integration scenarios both on-premises and cloud for various clients, each with different scenarios from a technical point of view, size, and criticality, using Microsoft Azure, Microsoft BizTalk Server and different technologies like AS2, EDI, RosettaNet, SAP, TIBCO etc.

He is a regular blogger, international speaker, and technical reviewer of several BizTalk books all focused on Integration. He is also the author of the book “BizTalk Mapping Patterns & Best Practices”. He has been awarded MVP since 2011 for his contributions to the integration community.
View all posts by Sandro Pereira