Convert DateTime to milliseconds

I see a lot of posts in this forum explaining how to do this, but I can’t get mine to work right. My variable DateTime is of type System.DateTime. That variable is converting a string to a date/time. I am trying to get DateTime to show as milliseconds.

Any help would be greatly appreciated.

image

@Josh_James You just want to convert your datetime value to milliseconds? can you clarify a bit? When you convert datetime to milliseconds, it will be in long format. You can treat it as text or string that also works. is there a specific reason why you are trying to store it in datetime variable?
ConvertToMilliSec.zip (2.4 KB)
To simply convert datetime to millisec the uploaded package works

1 Like

Hi @Josh_James
Assuming you have a DateTime variable named “DateTime”. Create a Long variable named "milliseconds"with DataType: System.Int64 and give below condition.

milliseconds = myDateTime.Ticks / 10000

Hope it helps

1 Like

Hi @Josh_James

Try as below

Create a variable of type DateTimeOffSet let’s say variable is dateTimeOffset, write below expression

dateTimeOffSet = New DateTimeOffSet(Cdate(Date as String))

Example: New DateTimeOffset(Cdate(Now.Tostring(“MM-dd-yyyy”)))

Now declare another variable of type Int64 let say unixTimestampMilliseconds, Now write expression as below

unixTimestampMilliseconds = dateTimeOffset.ToUnixTimeMilliseconds()

Hope this may help you

Thanks,
Srini

1 Like

Hello @Athira - This is great, thank you for this! Do you know how to make this work without the timestamp? Just get the milliseconds of the date itself?

@Josh_James the same would work for date also. Incase you have a datime value and you want to get the date only you can do it in many ways. You can use convert.tostring(“yourformat”) or for datetime variable variable.date should also return the date. You can check the uploaded file as a sample
ConvertToMilliSec.zip (2.4 KB)

Hope it helps

1 Like

Thank you for the help! This does work. I just need to play around a bit to see why my API is returning a slightly different millisecond result.

1 Like

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