Convert millis to date

Hi!
Some days ago i started developing an app with thunkable x, i was trying to convert millis (seconds since 1970 * 1000) into a date in format “dd/MM/yyyy HH:mm”, so i looked into clock component but i haven’t found the block as in thunkable classic.
Does anyone know how to do that?
Thank you.

1 Like

Hi there,

Is there a reason you want to do this instead of getting ‘current day/month/year hour/minute’ from the Device drawer of blocks?

You’d want to make some variables that store the number of ms in a year, a day, an hour and a minute. If there are x ms in a year, then the amount of years since 1970 is [round down] (ms since 1970 / x) and the amount of days, hours and minutes is [remainder of](ms since 1970 / x). Then do the same for days, get the date from that (counting from January 1st), then do the same for hours and minutes (counting from midnight).

Does that help?

1 Like

@jane

Sometimes you need to parse the time of a past event, a moment that was saved in a database, so it is not always “current”. I have a lot of that in my apps, and that is one reason X is not yet ready for those.

1 Like

Thanks for the insight!

I will try doing this, if it will work i’ll publish the code here :smiley:

1 Like

That’s my case

1 Like

Sounds great! Let me know if you have any other questions. :slightly_smiling_face:

I tried any way, but it doesn’t work… i thought about an online api service, but i haven’t found one, could someone help me?

If you haven’t found an on-line API for this, then perhaps there isn’t one.

But if you are willing to put the time into it, you can make the calculation yourself.
First, we have to put a few things as given: January 1st 1970 was a Thursday. That will serve as a start point reference if you want to get the day of an arbitrary date.

You will need a look-up map of the number of days each month has, i.e. 31, 28, 31, 30, etc.
But you also need to adjust the second entry of it as a function of the year.
If the year (which we will determine later) is divided by 400, it is a leap year, and therefore February has 29 day. But if it is divisible by 100, it was NOT a leap year. And if it is not divisible by 100 but is divisible by 4, it is a leap year.
So, the year 1800, 1900, 2100: not leap year
2000 and 2400: leap year.
All other years divisible by 4 (1972, 1976… 2016, 2020): leap years.

This means you will need a rather complex build that has those nested “if/else if” logic.

Bottom line is that each non-leap year will contribute 31536000000 millisecond.
And each leap year 31622400000.

So, you can start by turning the milliseconds into days, by dividing by 86400000.
Knowing how many days allows you to subtract them in chunks of 366 and 365 days to account for leap and regular years until you get a result that is less than 365 or 366 days.
Then, using your knowledge of which year that is, you can subtract the number of days per month–adjusting February as 28 or 29 as a function of the year–until you have less days that the next full month. That is, if you just subtracted March and its 31 days and are left with 28, which is less than April’s 30 days, then that is your date: 28th of April.
Then, with the fraction of a day from the original millisecond, you can derive the hour, minute, second.

And if you take the number of full days calculated at the start, add 3 (because January 1st 1970 was a day 4…), and get modulo 7 of it, the result will be a value between 0 and 6, with 0 being Sunday and 6 Saturday. So you could have the date and the day of the week.

As I warned, it is a bit complicated. But for the record, whoever programmed the clock component had to do it that way, so it is doable.

2 Likes

I’ve done almost all the things you wrote, but the result is wrong…
If thunkable x allowed extensions i would create one, because (i think) in this case java/other language coding is easier than block/visual coding.

I don’t know where’s the error, when i’ll get back home i will send the project here, so you could look at it.

Hey @Futuredev and @CBVG

i wonder if either of you would be willing to post the blocks you have used to accomplish this? Lot’s of us would find that helpful probably!

and of course, Thank you!

I tried this too and gave up.
If you are successful, I would be grateful if you showed me block coding.

Unfortunately i haven’t been able to find a solution :sleepy: :sweat_smile:

My blocks are in Classic Thunkable, so will require adaptation if you insist on using them in X:

Hello all,

I will make a tutorial on this eventually, but here is an API to convert EPOCH/UNIX time to a timestamp which can help us all organize data more easily. It’s super easy to use and only really requires an HTML request

http://unixtime.co.za/

@Futuredev @CBVG @chris.bailey230698t

for example
https://showcase.linx.twenty57.net:8081/UnixTime/fromunix?timestamp=***1549874568***

returns : 2019-02-11 08:42:48

some simple parsing of the respone and ta-da!

4 Likes

I need the tutorial :sob: PLEASE :sob: :sob: :sob:

1 Like