Method for converting HubSpot Unix millisecond TimeStamp to Asp.net DateTime

I searched all over, but couldn’t find a real easy way to do this. Here is what I came up with.

As you can see, I’m taking the string Value and converting it into a long. I’m then using FromUnixTimeMilliseconds to covert the long into DateTime. This works for me. Thought I would share it.

public DateTime CreatedOn
{
 get
 {
 long.TryParse(Value, out long timeStamp);

 return DateTimeOffset.FromUnixTimeMilliseconds(timeStamp).UtcDateTime;
 }
}

Hi, @dumber_texan2 :waving_hand: Thank you very much for sharing! — Jaycee

The Accepted answer is part of the initial post, but please feel free to add other solutions that work. In the accepted answer, Value is a string. I’ve seen other areas of the API where TimeStamp is handled as a long. In that case, no need to convert string to a long.

Hi @dumber_texan2

It looks like you have implemented a way to convert a Unix timestamp to a `DateTime` object in C#. The `FromUnixTimeMilliseconds` method is a convenient way to convert a Unix timestamp that is in milliseconds to a `DateTimeOffset` object, which can then be converted to a `DateTime` object using the `UtcDateTime` property.

Your implementation using `long.TryParse()` to parse the Unix timestamp from a string value before converting it to a `DateTime` object is a valid approach. However, you may want to add some error handling to handle cases where the input value is not a valid Unix timestamp string.

Here’s an updated version of your code with some error handling added:

```
public DateTime CreatedOn
{
get
{
if (long.TryParse(Value, out long timeStamp))
{
return DateTimeOffset.FromUnixTimeMilliseconds(timeStamp).UtcDateTime;
}
else
{
// Return a default value or throw an exception
return DateTime.MinValue;
}
}
}
```

In this updated version, if the `long.TryParse()` method fails to parse the Unix timestamp from the input value, the `DateTime.MinValue` value is returned as a default value. You can also choose to throw an exception instead if you want to handle the error in a different way.

Hope this will helps you out.If, you find it helpful, Please mrk it as solution.

Thanks!

Thanks!